我只想将 TextBox 控件的内容写入 Web 服务器目录根目录中的文件...如何指定它?
请记住,我正在本地测试这个...它不断地将文件写入我的程序文件\ Visual Studio \ Common \ IDE目录而不是我的项目目录(这是我假设Web服务器启动时root所在的位置) 。
我的问题是否与在 web.config 中指定正确的位置有关?我试过了,还是不行...
非常感谢...
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 采用虚拟路径并返回绝对路径。 “~”用于解析到应用程序根目录。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)