我就是想在服务器的根目录上写入一个文本,这段代码怎么错了?
<%@ 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;
}
}}
<%@ 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;
}
}}
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();
测试这段代码是可以生成文件的哦
你需要知道 .Server.MapPath 指定的是什么路径
而不是乱用