-2

I want to move several files that names are saved in an ObservableCollection<String> _collection with this method:

string firstFolderThatContainsEveryFile = "...\Folder\Files";
string secondFolderArchiv = "...\Folder\Files\Archiv";
foreach (var item in _collection)
{
     string firstFolder = System.IO.Path.Combine(firstFolderThatContainsEveryFile, item);
     string secondFolder = System.IO.Path.Combine(secondFolderArchiv, item);
     File.Move(firstFolder, secondFolder);
}

This works at the first time, but if i load new files into firstFolderThatContainsEveryFile and try to use my move method i get an exception:

File already in Use by other process

This are the steps: I open the programm -> use the move method -> success -> close the programm -> fill the folder with new files -> open the programm -> use the move method -> exception!

How can i get the processname or processID to close the process before i use my move method, or is there even a better way to get around this?

Liam
  • 27,717
  • 28
  • 128
  • 190
Raizzen
  • 192
  • 13
  • Is your collection empty before you add files to it the second time? If not your code will try to move the same files again, which will certainly cause an error like this. – heijp06 Jul 18 '18 at 07:40
  • @PetervanderHeijden jes, my collection is empty. I open the programm -> use the move method -> success -> close the programm -> fill the folder with new files -> open the programm -> use the move method -> exception! – Raizzen Jul 18 '18 at 07:43
  • @Liam omg jes you are right, iam sry i edit my question – Raizzen Jul 18 '18 at 07:52
  • Possible duplicate of [How do I find out which process is locking a file using .NET?](https://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net) – Rotem Jul 18 '18 at 07:54
  • 1
    Your first port of call is [identifying what is locking the file](https://stackoverflow.com/questions/241178/command-line-tool-for-finding-out-who-is-locking-a-file). If your closing the program it shouldn't be you, I can't see any thing in your code that would do this either. I'd imagine it's virus software or the like – Liam Jul 18 '18 at 07:56
  • @Liam i mean, that i close the programm manually, it does not close suddenly – Raizzen Jul 18 '18 at 07:59

1 Answers1

0

To figure out which process are using your file, with the solution proposed by this, you can use a tool Handle of Microsoft and this code C# to invoke the tool.

        public void ViewProcess(string filePath)
        {
            Process tool = new Process();
            tool.StartInfo.FileName = "handle.exe";
            tool.StartInfo.Arguments = filePath + " /accepteula";
            tool.StartInfo.UseShellExecute = false;
            tool.StartInfo.RedirectStandardOutput = true;
            tool.Start();
            tool.WaitForExit();
            string outputTool = tool.StandardOutput.ReadToEnd();

            string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)";
            foreach (Match match in Regex.Matches(outputTool, matchPattern))
            {
                try{                  
                   Console.WriteLine(match.Value); // this is the process ID using the file
                }
                catch(Exception ex)
                {
                }
            }
        }

If the file is using by the others program, you should figure why they use it, if by your program, so recheck your code to understand why.

Antoine V
  • 6,998
  • 2
  • 11
  • 34
  • he is inside a foreach loop, so you just wan't to terminate the process of his program? this is unsafe approach and a bad practice as well. – Barr J Jul 18 '18 at 08:23
  • @BarrJ that why I tell him to use the code to debug to see which process locks the file and figure out. And why do you sure that his program is locking the file ? – Antoine V Jul 18 '18 at 08:24
  • Because, he is using the file without checking if the file is locked. Terminating a process manually through code will never be good practice and you should avoid it as much as you can. – Barr J Jul 18 '18 at 08:27
  • This seems very dangerous. Better find out *why* the file is locked. – usr Jul 18 '18 at 08:27
  • @BarrJ not totally agree but i still updated the answer – Antoine V Jul 18 '18 at 08:32