Cannot Delete a File
-
I need to be able to delete a log file. However i'm getting the following error: {System.IO.IOException: The process cannot access the file 'C:\Web_Projects\eServiceRemoteDesktop\bin\Debug\mServiceLog.log' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path) at eServiceRemoteDesktop.frmTimeTicketSync.tlStpBtnClearLog_Click(Object sender, EventArgs e) in C:\Web_Projects\eServiceRemoteDesktop\frmTimeTicketSync.cs:line 229} (line 229 being the line where i'm trying to do the abovementioned delete file. The emphasis added in the snippet is by me.) I think that the Logger class we are using does not let go of the log file. I'm not going to be able to make any changes to the logger code though. Is there any other way to make this delete happen?
Two Thumbs Up!! Mihir Karkare
-
I need to be able to delete a log file. However i'm getting the following error: {System.IO.IOException: The process cannot access the file 'C:\Web_Projects\eServiceRemoteDesktop\bin\Debug\mServiceLog.log' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path) at eServiceRemoteDesktop.frmTimeTicketSync.tlStpBtnClearLog_Click(Object sender, EventArgs e) in C:\Web_Projects\eServiceRemoteDesktop\frmTimeTicketSync.cs:line 229} (line 229 being the line where i'm trying to do the abovementioned delete file. The emphasis added in the snippet is by me.) I think that the Logger class we are using does not let go of the log file. I'm not going to be able to make any changes to the logger code though. Is there any other way to make this delete happen?
Two Thumbs Up!! Mihir Karkare
Hello, Looks like your Logger class uses some kind of Stream, which doesn't get closed. This is not good practice. It is sometimes the case if an exception is thrown during stream write, your file stays also locked. For this cases you could use an external program which unlocks the file (Only use it if it's really an "emergency") http://www.chip.de/downloads/c1_downloads_18414122.html[^] You could start this over the Process and ProcessStartInfo class of System.Diagnostics.
unlockerprocess.FileName = "unlocker.exe"; unlockerprocess.Arguments = filename + " /s /o";
All the best, Martin
-
I need to be able to delete a log file. However i'm getting the following error: {System.IO.IOException: The process cannot access the file 'C:\Web_Projects\eServiceRemoteDesktop\bin\Debug\mServiceLog.log' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path) at eServiceRemoteDesktop.frmTimeTicketSync.tlStpBtnClearLog_Click(Object sender, EventArgs e) in C:\Web_Projects\eServiceRemoteDesktop\frmTimeTicketSync.cs:line 229} (line 229 being the line where i'm trying to do the abovementioned delete file. The emphasis added in the snippet is by me.) I think that the Logger class we are using does not let go of the log file. I'm not going to be able to make any changes to the logger code though. Is there any other way to make this delete happen?
Two Thumbs Up!! Mihir Karkare
If you programmatically fail to delete a file, insert a Thread.Sleep(5000); just before your File.Delete(). If that helps, ask and I will repeat the explanation upon your request.
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
I need to be able to delete a log file. However i'm getting the following error: {System.IO.IOException: The process cannot access the file 'C:\Web_Projects\eServiceRemoteDesktop\bin\Debug\mServiceLog.log' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path) at eServiceRemoteDesktop.frmTimeTicketSync.tlStpBtnClearLog_Click(Object sender, EventArgs e) in C:\Web_Projects\eServiceRemoteDesktop\frmTimeTicketSync.cs:line 229} (line 229 being the line where i'm trying to do the abovementioned delete file. The emphasis added in the snippet is by me.) I think that the Logger class we are using does not let go of the log file. I'm not going to be able to make any changes to the logger code though. Is there any other way to make this delete happen?
Two Thumbs Up!! Mihir Karkare
My dear you want to delete such a file that is already in use by any other process. First of all you should check which process using your desire file. For check the process you can use unlocker softwares. The second reason that i guess you have did that thing. That is you open the stream and did not close the stream. You should check your code that you create the file you must close the stream before using the the file by any other function. If you did't solve the problem then contact me personally nidostyle@gmail.com :)
-
I need to be able to delete a log file. However i'm getting the following error: {System.IO.IOException: The process cannot access the file 'C:\Web_Projects\eServiceRemoteDesktop\bin\Debug\mServiceLog.log' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.Delete(String path) at eServiceRemoteDesktop.frmTimeTicketSync.tlStpBtnClearLog_Click(Object sender, EventArgs e) in C:\Web_Projects\eServiceRemoteDesktop\frmTimeTicketSync.cs:line 229} (line 229 being the line where i'm trying to do the abovementioned delete file. The emphasis added in the snippet is by me.) I think that the Logger class we are using does not let go of the log file. I'm not going to be able to make any changes to the logger code though. Is there any other way to make this delete happen?
Two Thumbs Up!! Mihir Karkare
You first need to stop the service which is using this log file and then delete the log and restart the service. You can stop and restart the service using WMI approach. i can write down the code for you if you want, its quite simple. SS
-
If you programmatically fail to delete a file, insert a Thread.Sleep(5000); just before your File.Delete(). If that helps, ask and I will repeat the explanation upon your request.
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Ill try that. Anyway it turns out i may not have to delete hat file any way. But ill try doing this. Im curios to know why this works though.
Two Thumbs Up!! Mihir Karkare
-
My dear you want to delete such a file that is already in use by any other process. First of all you should check which process using your desire file. For check the process you can use unlocker softwares. The second reason that i guess you have did that thing. That is you open the stream and did not close the stream. You should check your code that you create the file you must close the stream before using the the file by any other function. If you did't solve the problem then contact me personally nidostyle@gmail.com :)
Hey thanks for helping but the requirement has changed. Turns out i dont have to delete the file anyway. Thanks for benig so helpful.
Two Thumbs Up!! Mihir Karkare
-
You first need to stop the service which is using this log file and then delete the log and restart the service. You can stop and restart the service using WMI approach. i can write down the code for you if you want, its quite simple. SS
Thank you so much. But turns out i dont have to perform that deletion anyway. If you could, please share the code anyway! thanks
Two Thumbs Up!! Mihir Karkare