Creating new directories via code...

Discussion in 'ASP.NET 2.0' started by codemonkey21, Feb 25, 2007.

  1. Everytime I create a new property for accessing our web site, I want to create a directory for it to store related files that will be uploaded.

    See the below code:




    // Create the GUID for the new property's ID


    Guid g = Utilities.getGUID();


    // Create a new directory on the server for the files related to the property


    try


    {


    string path = Server.MapPath("\\Files") + g.ToString();


    DirectoryInfo dir = Directory.CreateDirectory(path);


    }


    catch (UnauthorizedAccessException ex)


    {


    Utilities.LogError(ex);


    }


    The code doesn't give an error or produce a new directory... I'm very stumped. Could this be permissions related?
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    you should always get an error if there's some problem. I think your code may have just trapped the error.


    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  3. try adding another general catch block below your specific one and see if anything pops up.


    catch (UnauthorizedAccessException ex)


    {


    Utilities.LogError(ex);


    }





    catch (Exception ex)


    {


    Systems.Diagnostics.Debug.Assert(false,"Catch my silent error here!");


    }
     
  4. Your path is incorrect.

    Try :
    Guid g = Utilities.getGUID();

    // Create a new directory on the server for the files related to the property

    try
    {
    string path = MapPath('Files') +'\\'+ g.ToString();
    DirectoryInfo dir = Directory.CreateDirectory(path);
    }
    catch (UnauthorizedAccessException ex)
    {
    Utilities.LogError(ex);
    }

    Vikram

    DiscountASP.NET
    www.DiscountASP.NET
     

Share This Page