Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Cannot Delete a File

Cannot Delete a File

Scheduled Pinned Locked Moved C#
debugginghelpquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mihir Karkare
    wrote on last edited by
    #1

    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

    M L N S 4 Replies Last reply
    0
    • M 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

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M 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

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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] }


        M 1 Reply Last reply
        0
        • M 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

          N Offline
          N Offline
          Naveed727
          wrote on last edited by
          #4

          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 :)

          M 1 Reply Last reply
          0
          • M 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

            S Offline
            S Offline
            Software_Specialist
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • L Luc Pattyn

              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] }


              M Offline
              M Offline
              Mihir Karkare
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • N Naveed727

                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 :)

                M Offline
                M Offline
                Mihir Karkare
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • S Software_Specialist

                  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

                  M Offline
                  M Offline
                  Mihir Karkare
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups