写文件的Web服务器 - ASP.NET [英] Writing file to web server - ASP.NET

查看:133
本文介绍了写文件的Web服务器 - ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想一个TextBox控件的内容写入到Web服务器的根目录下的文件......我怎么规定的?

记住,我在本地\\ COMMON \\ IDE目录,而不是我的项目目录(这是我认为根源是当Web服务器测试这个......它使文件写入我的程序文件\\视觉工作室触发关闭)。

请问我的问题有些事情要在我的web.config中指定正确的位置?我试过了,仍然没有去...

谢谢了...


保护无效TestSubmit_ServerClick(对象发件人,EventArgs的发送)
    {
        StreamWriter的_testData =新的StreamWriter(data.txt中,真正的);
        _testData.WriteLine(TextBox1.Text); //写入文件。
        _testData.Close(); //关闭的StreamWriter实例。
        _testData.Dispose(); //从内存中进行处理。
    }


解决方案

 保护无效TestSubmit_ServerClick(对象发件人,EventArgs的发送)
{
  使用(StreamWriter的_testData =新的StreamWriter(使用Server.Mappath(〜/ data.txt中),真))
 {
  _testData.WriteLine(TextBox1.Text); //写入文件。
 }
}

使用Server.Mappath需要一个虚拟路径和返回一个绝对的。 〜用于解决应用程序根目录。

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.       
    }

解决方案

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.

这篇关于写文件的Web服务器 - ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆