Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,742
    Thanks
    41
    Thanked 191 Times in 190 Posts

    file path points to wrong folder dir

    I have a couple of functions... one (the one that is working) oploads a file to a local project folder... the code looks like this
    Code:
    string fileDirectory = @"/Files/";
                string FilePath = Request.PhysicalApplicationPath;
                if (FileUpload1.HasFile)
                {
                    string fileName = txt_fileName.Text + ".html" ;
                    string SaveFilePath = FilePath + fileDirectory + Server.HtmlEncode(fileName);
                    FileUpload1.SaveAs(SaveFilePath);
                }
    Now this works fine. I am assuming that it is pointing to the correct folder due to the method of request.physicalapplicationpath... it then uses that call to point to where to write the file to. I then have another function to delete files from that folder
    Code:
    string[] filepaths = Directory.GetFiles(@"/Files/");
                foreach (string filepath in filepaths)
                    File.Delete(filepath);
    now when i try this i get an error
    System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Files\'.
    now this to me is obvious, i am calling the directory in directory.getfiles...
    but i cannot figure out how to set my directory path relative to my application path...
    IE how do i do something similar to request.physicalapplicationpath and apply it to my delete call... I've tried various methods but i get error after error...
    Last edited by alykins; 10-17-2011 at 05:03 PM.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    You didn't use PhysicalApplicationPath in the second set of code!!!

    So it *assumes* C:\ as the physical path, hence the error.

    Code:
    string rootPath = Request.PhysicalApplicationPath;
    string fileDirectory = @"/Files/";
    string basePath = rootPath + fileDirectory;
    // debug to make sure basePath is correct
    
    string[] filepaths = Directory.GetFiles(basePath);
    foreach (string filepath in filepaths)
          File.Delete(filepath);
    No?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    alykins (10-17-2011)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    Oh, doh on me! You don't *HAVE* a valid Request in that second set of code, as that's running batch or some such, is that right???

    You may want to use Server.MapPath in *both* cases, then. But, again, Sever is only valid for ASP.NET coding. If you are truly running a standalone application, then you have have to find another way.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,742
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Thanks for getting back; I think your first post will resolve the issue. I didn't realize I wasn't setting the path until I looked at what you posted (feel like a putz now). Thank you thank you!!

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •