2

I have a file i want to close the file programmatically which is used by another process.

The thing is i want to free the file from another process whichever is using that and then delete it.. Anyway to do that programmatically? i am using c#..

how to delete this??

i have done a code like,

if (File.Exists(filePath)) File.Delete(filePath);

to delete the file

user251334
  • 433
  • 2
  • 5
  • 6
  • Related: http://stackoverflow.com/questions/1977611/is-it-possible-to-bypass-a-file-lock-in-c-when-another-thread-process-is-unecess – H H Feb 11 '10 at 12:57

5 Answers5

4

It depends on the process. You don't necessary have the rights to shut down the other process using the file, and it could be potentially dangerous.

You can consider using http://msdn.microsoft.com/en-us/library/ms686714(VS.85).aspx to terminate the process once you know it's handle (Use P/Invoke to access it from C#)

Edit: Also see following question to help determind the handle of the process locking up your file, Using C#, how does one figure out what process locked a file?

Community
  • 1
  • 1
Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
3

I'm not sure this is a good idea... It would depend on what the 'other' application is doing with the file and whether it would have any impact on system or data stability / integrity.

Having said that, you might be able to get the functionality you require by shelling out to something like Sysinternals Handle to get the identity of the other program and force it to release the file by whatever means are necessary.

My preferred option, though, would be to go into a waiting loop until the file has been freed by the other app, and then remove it. That's just me, though.

--EDIT-- This idea is further complicated if the file in question is on a network share - in that case you wouldn't know what process is locking the file as it might not be on your machine. (Actually, I'm not sure how various kinds of file share lock files that are in use - more complication ;) )

ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
2

Try this.

Though I would not recommend closing handles of other processes unless you REALLY know the consequences of doing that!

Community
  • 1
  • 1
Jason Evans
  • 28,906
  • 14
  • 90
  • 154
1

You may not have any right to do so and anyway you will get an exception thrown as a result by your code sample. Programmatically, under C#, there would be a lot of pinvokes to acquire security credentials and to allow your program to swipe the file from another process, using process injection to lift the process's own dead fingers off the file - which comes back to this - Why would you want to do that to another process anyway?

That is very suspicious if you don't me saying...You could cause a program corruption within that process (it could be using it for their own reason) and if you tried, that process will end up failing because that resource is no longer there...

Hope this helps, Best regards, Tom.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
0

If your file is text file then you don't need to delete your file (it's a bad idea to exit a process from your code), rather you can make your text file empty.

if (File.Exists(filePath)){
  File.WriteAllText(filePath, String.Empty);
 }
sakib rahman
  • 172
  • 1
  • 14