Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

c# - Writing file to web server - ASP.NET

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 filesvisual studioCommonIDE 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.       
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...