我就是想在服务器的根目录上写入一个文本,这段代码怎么错了?
<%@ 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.   

    1:http://msdn.microsoft.com/zh-cn/library/3zc0w663(v=vs.80).aspx2:站点所在的文件夹目录有写的权限?
      

  2.   

    string strFilePath = HttpContext.Current.Server.MapPath("~/1.txt");
      

  3.   

     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();
        
    测试这段代码是可以生成文件的哦
      

  4.   

    动态改变的文件,应该保存在 app_data 目录下。例如:string strFilePath = HttpContext.Current.Server.MapPath("~/app_data/1.txt");假设在网站其它目录下写,可能会造成随后asp.net程序重启,你的什么session集合、静态变量之类的,立刻就全都丢失了!
      

  5.   


    你需要知道 .Server.MapPath 指定的是什么路径
    而不是乱用