C# ASP 在服务端写入一个文本
我就是想在服务器的根目录上写入一个文本,这段代码怎么错了?<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.IO;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write(context.Request["username"]);
context.Response.Write("\n");
context.Response.Write(context.Request["password"]);
string strFilePath = HttpContext.Current.Server.MapPath("/1.txt");
if (!System.IO.File.Exists(strFilePath))
{
System.IO.FileStream f = System.IO.File.Create(strFilePath);
f.Close();
}
System.IO.StreamWriter f2 = new System.IO.StreamWriter(strFilePath, true, System.Text.Encoding.GetEncoding("gb2312"));
f2.WriteLine("嘿嘿");
f2.Close();
f2.Dispose();
}
public bool IsReusable {
get {
return false;
}
}
} --------------------编程问答-------------------- 现在什么问题,调试过吗 --------------------编程问答-------------------- 1:http://msdn.microsoft.com/zh-cn/library/3zc0w663(v=vs.80).aspx
2:站点所在的文件夹目录有写的权限? --------------------编程问答-------------------- --------------------编程问答-------------------- string strFilePath = HttpContext.Current.Server.MapPath("~/1.txt"); --------------------编程问答-------------------- --------------------编程问答-------------------- 什么问题呢,说出来,大家研究一下 --------------------编程问答-------------------- string strFilePath = HttpContext.Current.Server.MapPath("/1.txt");
if (!System.IO.File.Exists(strFilePath))
{
System.IO.FileStream f = System.IO.File.Create(strFilePath);
f.Close();
}
System.IO.StreamWriter f2 = new System.IO.StreamWriter(strFilePath, true, System.Text.Encoding.GetEncoding("gb2312"));
f2.WriteLine("嘿嘿");
f2.Close();
f2.Dispose();
测试这段代码是可以生成文件的哦 --------------------编程问答-------------------- 可以嘛?那我怎么找不到生成的这个文件呢? --------------------编程问答-------------------- 动态改变的文件,应该保存在 app_data 目录下。例如:
string strFilePath = HttpContext.Current.Server.MapPath("~/app_data/1.txt");
假设在网站其它目录下写,可能会造成随后asp.net程序重启,你的什么session集合、静态变量之类的,立刻就全都丢失了! --------------------编程问答--------------------
在bin目录下 --------------------编程问答-------------------- --------------------编程问答--------------------
顶 --------------------编程问答--------------------
你需要知道 .Server.MapPath 指定的是什么路径
而不是乱用
补充:.NET技术 , ASP.NET