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. Visual Basic
  4. Problem deleting a file (being used by another process)

Problem deleting a file (being used by another process)

Scheduled Pinned Locked Moved Visual Basic
helpannouncement
5 Posts 4 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.
  • C Offline
    C Offline
    Central_IT
    wrote on last edited by
    #1

    Hi, I get the following message when I try and delete a file. "System.IO.IOException - The process cannot access the file 'filename' because it is being used by another process." My cut down code is as follows Dim aryFi As IO.FileInfo() = diFolder.GetFiles("*.pdf") For Each fi In aryFi sFileName = fi.Name fi.delete Next Basically what I am doing is that depending what file is in the folder, delete it once I get various information about it that file. The above code is a cut down version. What other way would you delete a file or is this the best way. If so, what am I doing wrong. Thanks in advance.

    modified on Wednesday, May 25, 2011 8:30 AM

    D L 2 Replies Last reply
    0
    • C Central_IT

      Hi, I get the following message when I try and delete a file. "System.IO.IOException - The process cannot access the file 'filename' because it is being used by another process." My cut down code is as follows Dim aryFi As IO.FileInfo() = diFolder.GetFiles("*.pdf") For Each fi In aryFi sFileName = fi.Name fi.delete Next Basically what I am doing is that depending what file is in the folder, delete it once I get various information about it that file. The above code is a cut down version. What other way would you delete a file or is this the best way. If so, what am I doing wrong. Thanks in advance.

      modified on Wednesday, May 25, 2011 8:30 AM

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      The "other process" still has the file open. There's no way to delete it until the other process closes the file. You could force it closed using some hacked techniques, but that'll only crash the other process that still has it open.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      1 Reply Last reply
      0
      • C Central_IT

        Hi, I get the following message when I try and delete a file. "System.IO.IOException - The process cannot access the file 'filename' because it is being used by another process." My cut down code is as follows Dim aryFi As IO.FileInfo() = diFolder.GetFiles("*.pdf") For Each fi In aryFi sFileName = fi.Name fi.delete Next Basically what I am doing is that depending what file is in the folder, delete it once I get various information about it that file. The above code is a cut down version. What other way would you delete a file or is this the best way. If so, what am I doing wrong. Thanks in advance.

        modified on Wednesday, May 25, 2011 8:30 AM

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

        Hi, that is one of the more confusing Windows/.NET error messages. The "other process" may well be your own process, it really should say "some process". So it could be as well your own process having read or written the file, or having interrogated the file system about its characteristics. Make sure you don't have open streams, files, fileinfo's, etc; close and dispose them before executing the code that throws the exception. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
        Please use <PRE> tags for code snippets, they improve readability.
        CP Vanity has been updated to V2.3

        N 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, that is one of the more confusing Windows/.NET error messages. The "other process" may well be your own process, it really should say "some process". So it could be as well your own process having read or written the file, or having interrogated the file system about its characteristics. Make sure you don't have open streams, files, fileinfo's, etc; close and dispose them before executing the code that throws the exception. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
          Please use <PRE> tags for code snippets, they improve readability.
          CP Vanity has been updated to V2.3

          N Offline
          N Offline
          Nagy Vilmos
          wrote on last edited by
          #4

          Maybe it should just say "... is currently in use."


          Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)

          L 1 Reply Last reply
          0
          • N Nagy Vilmos

            Maybe it should just say "... is currently in use."


            Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)

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

            Yes and no. Shortening the message is one way of improving the probability it hits the truth, but then it is also less informative. I'd rather have it say: "the file is currently in use, either by your own process or by some other process." That way, the reader knows he has to actually look for two quite different possibilities. And then, of course, they should put in some more effort and find out which of those two it is, and provide the more specific message; either "the file is currently in use by your own process", or "the file is currently in use by another process (processname, processID)". But that would be helping the user, something Windows isn't inclined to do much. :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
            Please use <PRE> tags for code snippets, they improve readability.
            CP Vanity has been updated to V2.3

            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