2

As part of a program (Visual Studio 2015 vb/c# in Windows 10 32 bit), I would like to delete a folder containing a file that is passed via the context menu. But it appears that because the file name is passed to the program via command line arguments, the folder then becomes locked by the program receiving it, so I am unable to delete it. To be more specific:

  1. I added a key to the registry for a certain file type (tgz.) So when I right-click on that file in windows explorer, I see the command that launches my program and can execute it. All that works fine.
  2. What the program that gets called does is to extract the contents of that file and move them somewhere else. The original folder then gets emptied of files. All this works fine. You therefore get left with an empty directory that once contained the tgz file.
  3. At the end of this program I would like to delete this empty directory. But when I attempt to do so, I get an exception "System.IO.IOException: The process cannot access the file ... because it is being used by another process." Using LockHunter (freeware) I see that the folder is locked by the current program. So until I exit the program I can't delete the folder!

I have already tried having the program call another one e.g. when it gets to the point where it needs to delete the directory, I instead shell (either via shell or process.start (in both cases with no waiting)) that gets passed the folder name and then the current program is ended - so the lock is removed. That second program runs in a loop trying to delete the directory. I give it 10 seconds to attempt to do this running in a loop, but that then also fails to delete the directory. Again using LockHunter I see that the 2nd program is now locking the folder. So back to square one. I also checked that it is not windows explorer that is locking the file, by automatically closing the explorer window containing the folder at the start of the program.

I have been struggling for days on this and would deeply welcome any suggestions!

Thanks! Zos

zos474
  • 21
  • 2
  • Are you disposing all the handle try using block in ur code which will release all locks . – loneshark99 Aug 28 '16 at 20:12
  • 4
    It might help to change the current directory away from the one you need to delete. See https://msdn.microsoft.com/en-us/library/system.io.directory.setcurrentdirectory%28v=vs.110%29.aspx – Peter Bill Aug 28 '16 at 21:05
  • 4
    Pay attention to the ProcessStartInfo.WorkingDirectory property. If it is the same as that directory then you can't delete the directory until the program terminates. Because, well, the program is working in it. Same is true for your own Environment.CurrentDirectory. – Hans Passant Aug 28 '16 at 21:45
  • The directory is probably locked by you because it is your current directory. Change your current directory to some other directory. – Raymond Chen Aug 29 '16 at 02:03
  • Peter, Hans and Raymond - thanks very much! Your suggestion of changing the current directory did the trick. By doing that (i.e. a call to Directory.SetCurrentDirectory([Different Directory Name]) at the start of the program) I was able to remove the tgz-containing directory at the end of the program & not have to shell to a second program. (Note: At the moment I dont have enough rep to be able to upvote or select the best answer, so can't officially select the best answer until I have more points here.) – zos474 Aug 29 '16 at 22:43
  • Possible duplicate of [How do I delete a file which is locked by another process in C#?](http://stackoverflow.com/questions/1040/how-do-i-delete-a-file-which-is-locked-by-another-process-in-c) – ivan_pozdeev Apr 12 '17 at 11:56

0 Answers0