How do I access the file system to create and store files?

Discussion in 'ASP.NET 2.0' started by norisk, Oct 6, 2007.

  1. Howdo I create a file using Streamwriter.createtext('c:\Temp\Myfile.txt') on the servers here and set permissions?

    Thanks,

    Bill Ross aka WoundedEgo.com
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    Try this :


    Streamwriter.createtext(server.mappath("\myfile.txt"))





    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  3. Thanks. That was almost it. I wound up with:





    StreamWriter sw = File.CreateText(MapPath("/Temp.txt"));

    Thanks again,

    Bill Ross
     
  4. string path = Server.MapPath(@"/App_Data/test.xml");
     
  5. Yes,you have equal permissions to all the folders within your application root directory .But remember as a security measure, files in the App_Data folder are not served by the Web server.

    Vikram

    DiscountASP.NET
    www.DiscountASP.NET
     
  6. Indeed, even the new version of the Msft Sync Toy is refused access to the localApp_Data folder.
     
  7. Ok, well I was afraid I was asking a stooopid question, but there are some lessons:


    * if you want to create temp files, use server.mappath(@"/app_data/")


    * if you need to access the file system and allow other apps to access the files, use server.mappath(@"/somethingotherthanapp_data/")


    Yes? Is that the bottom line?


    Thanks,


    Bill Ross


    http://bibleshockers.blogspot.com
     
  8. Hey Bill, just read some of your Blog posts, some good reading there bro. [​IMG]
    As for your site root question,try it like this instead:
    Server.MapPath("~/App_Data/yourtempfile.txt")

    That way, with~ asthe Application base path designator,you catch the correct site root App Data folder.

    In a data string |DataDirectory|\somefile will also catch somefile in the root App Data folder.

    It can be a lot of fun using that one.

    i.e.
    This connection string syntax is supported by the SqlClient and OleDb managed providers.
    By default, the |DataDirectory| variable will be expanded as follow:


    - For applications placed in a directory on the user machine, this will be the app's (.exe) folder.
    - For apps running under ClickOnce, this will bea special data folder created by ClickOnce
    - For Web apps, this will be the App_Data folder


    Under the hood, the value for |DataDirectory| simply comes from a property on the app domain. It is possible to change that value and override the default behavior by doing this:
    AppDomain.CurrentDomain.SetData("DataDirectory", newpath)<a target="_blank" href="https://blogs.msdn.com/smartclientdata/archive/2005/08/26/456886.aspx">
    https://blogs.msdn.com/smartclientdata/archive/2005/08/26/456886.aspx</A>
     
  9. Are there any directories with special privileges assigned, such as app_data, or do I have equal permissions anywhere I want to put a file?

    Thanks
     

Share This Page