我想在我的WEB服务器上写个错误日志他的路径是C:\Inetpub\wwwroot\TestDemo\Map下private void Button1_Click(object sender, System.EventArgs e)
{
string filed="\\"+DateTime.Now.ToString("yy-MM-dd")+"ErrLog.txt";
string path=Server.MapPath("/Map")+filed; if(File.Exists(filed))
{
FileStream fs=new FileStream(path,FileMode.Append,FileAccess.Write);
StreamWriter sw=new StreamWriter(fs);
sw.WriteLine(DateTime.Now+":"+"111");
sw.Flush();
sw.Close();
}
else
{
FileStream fs=new FileStream(path,FileMode.CreateNew,FileAccess.Write);
StreamWriter sw=new StreamWriter(fs);
sw.WriteLine(DateTime.Now+":"+"222");
sw.Flush();
sw.Close();
}
}老是说
“/TestDemo”应用程序中的服务器错误。
--------------------------------------------------------------------------------未找到路径“c:\inetpub\wwwroot\Map\05-12-19ErrLog.txt”的一部分。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IO.DirectoryNotFoundException: 未找到路径“c:\inetpub\wwwroot\Map\05-12-19ErrLog.txt”的一部分。源错误: 
行 63:  else
行 64:  {
行 65:  FileStream fs=new FileStream(path,FileMode.CreateNew,FileAccess.Write);
行 66:  StreamWriter sw=new StreamWriter(fs);
行 67:  sw.WriteLine(DateTime.Now+":"+"222");
 源文件: c:\inetpub\wwwroot\testdemo\map\getmap.aspx.cs    行: 65 堆栈跟踪: 
[DirectoryNotFoundException: 未找到路径“c:\inetpub\wwwroot\Map\05-12-19ErrLog.txt”的一部分。]
   System.IO.__Error.WinIOError(Int32 errorCode, String str) +287
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy) +888
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) +43
   TestDemo.Map.GetMap.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\testdemo\map\getmap.aspx.cs:65
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain() 这是怎么回事呀请大家帮帮忙哈

解决方案 »

  1.   

    if(File.Exists(filed))
    文件找不到你怎么执行
    你else{}里面的代码是给txt写东西。。都找不到文件了还写什么?
    string path=Server.MapPath("Map")+filed;试试
      

  2.   

    string filed="\\"+DateTime.Now.ToString("yy-MM-dd")+"ErrLog.txt";
    string path=Server.MapPath("/Map")+filed;
    的错误,路径不对
      

  3.   

    string filed="\\"+DateTime.Now.ToString("yy-MM-dd")+"ErrLog.txt";
    string path=Server.MapPath("/Map")+filed;if(!File.Exists(path))
    {
    File.Create(path);//若文件不存在就先创建
    } FileStream fs=new FileStream(path,FileMode.Append,FileAccess.Write);
    StreamWriter sw=new StreamWriter(fs);
    sw.WriteLine(DateTime.Now+":"+"111");
    sw.Flush();
    sw.Close();