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. Delete a File - "File does not exist"

Delete a File - "File does not exist"

Scheduled Pinned Locked Moved C#
questionhelp
6 Posts 5 Posters 3 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.
  • P Offline
    P Offline
    phisco123
    wrote on last edited by
    #1

    Hey Guys, I'm working on a webapplication and I'm saving files .pdf in a folder called "Files" inside of the application project... So, I display these pdf files but I got a nasty problem, with delete them, It's driving me nuts.... because I every time I try to do it, I get this exception: "File does not exist", It couldn't be found... BUT the goddamned File does exist.. In fact, I display this pdf file (with response.transmitfile) with the same string path and It works... but when I try to delete it with the methos from IO.. It doesn't work at all.. Here' part my code: string path = @"\Files" + "\\" +filename.pdf; if (File.Exists(path)) { File.Delete(path); elimino = true; } So, anybody knows how can I solve this problem??? Thanks...

    D H 2 Replies Last reply
    0
    • P phisco123

      Hey Guys, I'm working on a webapplication and I'm saving files .pdf in a folder called "Files" inside of the application project... So, I display these pdf files but I got a nasty problem, with delete them, It's driving me nuts.... because I every time I try to do it, I get this exception: "File does not exist", It couldn't be found... BUT the goddamned File does exist.. In fact, I display this pdf file (with response.transmitfile) with the same string path and It works... but when I try to delete it with the methos from IO.. It doesn't work at all.. Here' part my code: string path = @"\Files" + "\\" +filename.pdf; if (File.Exists(path)) { File.Delete(path); elimino = true; } So, anybody knows how can I solve this problem??? Thanks...

      D Offline
      D Offline
      Dr Walt Fair PE
      wrote on last edited by
      #2

      phisco123 wrote:

      string path = @"\Files" + "\\" +filename.pdf;
      if (File.Exists(path))

      Maybe that's just a typo when you posted the message, but do you even have a variable named filename.pdf? What is the value of your path variable? If the actual filename is left off, then the path would exist, but you would be trying to delete a directory, not an actual file. Of course, if it was just a typo in the message, it could be something else.

      CQ de W5ALT

      Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

      1 Reply Last reply
      0
      • P phisco123

        Hey Guys, I'm working on a webapplication and I'm saving files .pdf in a folder called "Files" inside of the application project... So, I display these pdf files but I got a nasty problem, with delete them, It's driving me nuts.... because I every time I try to do it, I get this exception: "File does not exist", It couldn't be found... BUT the goddamned File does exist.. In fact, I display this pdf file (with response.transmitfile) with the same string path and It works... but when I try to delete it with the methos from IO.. It doesn't work at all.. Here' part my code: string path = @"\Files" + "\\" +filename.pdf; if (File.Exists(path)) { File.Delete(path); elimino = true; } So, anybody knows how can I solve this problem??? Thanks...

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        Because you have a backslash at the beginning, which means the search starts from the root of the drive. For example, if your web root is locally on the C: drive, then you're code is looking for C:\Files\whatever. I doubt that's what you want. If your files root is under your web application root - not generally a good idea since people then have direct access at least for file extensions not configured with your server software (ex: IIS) - you can use ~ to resolve the path like so:

        // Assumes you're running this from within an ASP.NET Page
        string root = Server.MapPath("~/Files");
        string path = Path.Combine(root, filename);
        if (File.Exists(path))
        {
        File.Delete(path);
        }

        It's also a good idea to use Path.Combine, which works on any manage host environment (though Windows is most typical) and takes care of any trailing backslashes since double backslashes in some use cases can cause problems (referencing back to root, for example).

        This posting is provided "AS IS" with no warranties, and confers no rights. Program Manager II Visual Studio Professional Deployment Experience Microsoft [My Articles] [My Blog]

        E P 2 Replies Last reply
        0
        • H Heath Stewart

          Because you have a backslash at the beginning, which means the search starts from the root of the drive. For example, if your web root is locally on the C: drive, then you're code is looking for C:\Files\whatever. I doubt that's what you want. If your files root is under your web application root - not generally a good idea since people then have direct access at least for file extensions not configured with your server software (ex: IIS) - you can use ~ to resolve the path like so:

          // Assumes you're running this from within an ASP.NET Page
          string root = Server.MapPath("~/Files");
          string path = Path.Combine(root, filename);
          if (File.Exists(path))
          {
          File.Delete(path);
          }

          It's also a good idea to use Path.Combine, which works on any manage host environment (though Windows is most typical) and takes care of any trailing backslashes since double backslashes in some use cases can cause problems (referencing back to root, for example).

          This posting is provided "AS IS" with no warranties, and confers no rights. Program Manager II Visual Studio Professional Deployment Experience Microsoft [My Articles] [My Blog]

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          Don't forget ... MS best practice[sic] is merely: try{ File.Delete(path); } catch{} Considering the file can be deleted between the File.Exist and File.Delete calls by another process.

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

          H 1 Reply Last reply
          0
          • E Ennis Ray Lynch Jr

            Don't forget ... MS best practice[sic] is merely: try{ File.Delete(path); } catch{} Considering the file can be deleted between the File.Exist and File.Delete calls by another process.

            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

            H Offline
            H Offline
            Herboren
            wrote on last edited by
            #5

            The user could also try setting a static setting of the directory location within his application under compile options and then just specify the file name which he is trying to delete. Try creating a setting, string string ie: string FilePath = "%USEPROFILE%.\\Desktop\\DirectoryToFile\\" (or where ever your path is located) Then in your application just add the Filename to the path, so that way you know that your application is pointing to the location, it would be concrete. Of course however if it is still saying that the file doesnt exists, make sure you are spelling it correctly and including the applications extension(which I'm not saying you aren't) and make sure you use the Try::catch, which is vital in this situation.

            String FILE_TO_DELETE = FilePath+FileName;
            Try
            if.exists(FILE_TO_DELETE) then
            FILE_TO_DELETE.delete
            end if
            catch ex as exception
            msgbox("Unable to delete file. Reason: ", ex);
            end try

            And with the benefit of setting a static path in your program, you can change the path at anytime and save its settings easily. because as long as your code includes the "String FILE_TO_DELETE = FilePath+FileName;" That string will always change, just not your code.

            1 Reply Last reply
            0
            • H Heath Stewart

              Because you have a backslash at the beginning, which means the search starts from the root of the drive. For example, if your web root is locally on the C: drive, then you're code is looking for C:\Files\whatever. I doubt that's what you want. If your files root is under your web application root - not generally a good idea since people then have direct access at least for file extensions not configured with your server software (ex: IIS) - you can use ~ to resolve the path like so:

              // Assumes you're running this from within an ASP.NET Page
              string root = Server.MapPath("~/Files");
              string path = Path.Combine(root, filename);
              if (File.Exists(path))
              {
              File.Delete(path);
              }

              It's also a good idea to use Path.Combine, which works on any manage host environment (though Windows is most typical) and takes care of any trailing backslashes since double backslashes in some use cases can cause problems (referencing back to root, for example).

              This posting is provided "AS IS" with no warranties, and confers no rights. Program Manager II Visual Studio Professional Deployment Experience Microsoft [My Articles] [My Blog]

              P Offline
              P Offline
              phisco123
              wrote on last edited by
              #6

              Heath Stewart wrote:

              Because you have a backslash at the beginning, which means the search starts from the root of the drive. For example, if your web root is locally on the C: drive, then you're code is looking for C:\Files\whatever. I doubt that's what you want.
               
              If your files root is under your web application root - not generally a good idea since people then have direct access at least for file extensions not configured with your server software (ex: IIS) - you can use ~ to resolve the path like so:

              // Assumes you're running this from within an ASP.NET Page
              string root = Server.MapPath("~/Files");
              string path = Path.Combine(root, filename);
              if (File.Exists(path))
              {
              File.Delete(path);
              }

              It's also a good idea to use Path.Combine, which works on any manage host environment (though Windows is most typical) and takes care of any trailing backslashes since double backslashes in some use cases can cause problems (referencing back to root, for example).

              Thanks man, That was it

              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