1

This code is the first code in my Form_Load method:

DirectoryInfo dir =new DirectoryInfo("d:\\themes.thumb");

string[] animals = new string []
{
    "Snakes",
    "SnowyOwls",
    "Tigers",
    "TropicalFish",
    "WildBeauty",
    "Wolves"
};

foreach (FileInfo fil in dir.GetFiles())
{
    for(int ii=0;ii<animals.Length;ii++)
    {
        if (fil.Name.StartsWith(animals[ii]))
        {
        try
        {
            fil.Replace(fil.FullName,fil.FullName.Replace(fil.Name,"Animals-" + fil.Name));
        }
        catch
        {
        }
    }
}

and I'm getting the following error whenever if (fil.Name.StartsWith(animals[ii])) is true:

The process cannot access the file because it is being used by another process.

What is wrong as I have not opened any files before this code?

5
  • as an idea: maybe dir.GetFiles() creates a lock on the file? Commented Jun 5, 2015 at 6:40
  • well I first thought of that but then I thought it must be really silly if just getting the file list put lock on them as its supposed to be used like this Commented Jun 5, 2015 at 6:42
  • 1
    Does this code really compile for you? What are all those private scattered around? Commented Jun 5, 2015 at 6:49
  • 1
    What's up with all the private in the code? Does that even compile? Commented Jun 5, 2015 at 6:49
  • @nvoigt someone edited my question he must have add them there were no private in my code. I cleared them again Commented Jun 5, 2015 at 6:52

3 Answers 3

1

You should seperate your reading logic from your update logic.

for example:

var replacements = dir.GetFiles()
                      .Where(file => animals.Any(animal => file.Name.StartsWith(animal)))
                      .Select(file => new 
                                     {
                                       OldFullName = file.FullName, 
                                       NewFullName = file.FullName.Replace(file.Name, "Animals-" + file.Name) 
                                     })
                      .ToList();

foreach (var replacement in replacements)
{
    File.Move(replacement.OldFullName, replacement.NewFullName);
}

Your replace logic has some subtle bugs (what happens with files that are in a folder called "Wolves" for example?) you may wan to work that out.

Sign up to request clarification or add additional context in comments.

2 Comments

you mean that getting files locks it?
As your renaming of files actually invalidates the information in the array you got from GetFiles, I'll take a wild guess and say yes.
1

It looks like you are misunderstanding how to use the FileInfo.Replace method.

fil.Replace(fil.FullName,fil.FullName.Replace(fil.Name,"Animals-" + fil.Name));

Here you are actually trying to overwrite fil's contents with itself. That explains the error message.

You might want to read the documentation a bit more closely.

EDIT: To be absolutely clear: FileInfo.Replace is not meant to be used to perform file renames. It's meant to replace file contents. To perform a rename, you use FileInfo.MoveTo.

4 Comments

yes. the replace works as rename whenever the destination file is the file itself
Oh my. That's the worst way to do a rename, and causes the error message you encountered. Please don't do that.
well what would you offer for renaming?
fil.MoveTo(fil.FullName.Replace(fil.Name,"Animals-" + fil.Name);
0

Get LockHunter. It's a free tool which shows you which process is holding onto a particular file or folder. I found it really useful. Microsoft Process Explorer is also free and can also find open handles (Ctrl+F) by name.

1 Comment

the program is great but I want to know where the error is. but the programs only shows that which program is using hte file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.