47

I simply want to write the contents of a TextBox control to a file in the root of the web server directory... how do I specify it?

Bear in mind, I'm testing this locally... it keeps writing the file to my program files\visual studio\Common\IDE directory rather than my project directory (which is where I assume root is when the web server fires off).

Does my problem have something to do with specifying the right location in my web.config? I tried that and still no go...

Thanks much...

protected void TestSubmit_ServerClick(object sender, EventArgs e)
    {
        StreamWriter _testData = new StreamWriter("data.txt", true);
        _testData.WriteLine(TextBox1.Text); // Write the file.
        _testData.Close(); // Close the instance of StreamWriter.
        _testData.Dispose(); // Dispose from memory.       
    }
Woody
  • 1,159
  • 3
  • 15
  • 25
  • 9
    If this is more than a simple test project *never* save to the root of your Web server directory. Use a temp folder that is not accessible from the whole world, otherwise users can place arbitrary content on your server. – Dirk Vollmar Aug 12 '09 at 21:26

4 Answers4

80
protected void TestSubmit_ServerClick(object sender, EventArgs e)
{
  using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true))
 {
  _testData.WriteLine(TextBox1.Text); // Write the file.
 }         
}

Server.MapPath takes a virtual path and returns an absolute one. "~" is used to resolve to the application root.

Community
  • 1
  • 1
Darthg8r
  • 12,377
  • 15
  • 63
  • 100
  • 1
    As @SpencerRuport mentioned, it is not good idea to set write permission on root directory. It's better for example to create new folder with write permission and use it for all your outputs. – prespic Nov 06 '14 at 15:32
35

There are methods like WriteAllText in the File class for common operations on files.

Use the MapPath method to get the physical path for a file in your web application.

File.WriteAllText(Server.MapPath("~/data.txt"), TextBox1.Text);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    I have always used this method, and I am trying to use it now, but it doesn't fail when executing, and doesn't write anything to the file either!! I tried to check the file using the Exists method and it returns true, don't know what is the problem!! – Sisyphus Apr 09 '18 at 09:25
17
protected void TestSubmit_ServerClick(object sender, EventArgs e)
{
    using (StreamWriter w = new StreamWriter(Server.MapPath("~/data.txt"), true))
    {
        w.WriteLine(TextBox1.Text); // Write the text
    }
}
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • 23
    Note that Server.MapPath is `System.Web.HttpContext.Current.Server.MapPath` if you are writing code in the code behind. – styfle Aug 27 '12 at 19:12
9

Keep in mind you'll also have to give the IUSR account write access for the folder once you upload to your web server.

Personally I recommend not allowing write access to the root folder unless you have a good reason for doing so. And then you need to be careful what sort of files you allow to be saved so you don't inadvertently allow someone to write their own ASPX pages.

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147