当我使用FileStream结合StreamWriter去写".log"后缀名的文件时,会报错,说访问被拒绝,代码如下:
string path = HttpContext.Current.Server.MapPath("~/logs/"+DateTime.Now.ToString("yyyy-MM-dd")+"/");
if(!Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
path = path + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
System.IO.FileStream fs = new System.IO.FileStream(path,System.IO.FileMode.Append);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
sw.Write("订单没有会员号");
sw.Close();
fs.Close();
若只用StreamWriter= System.IO.File.AppendText(path)这个方法去写,则可以正确执行,代码如下:
string path = HttpContext.Current.Server.MapPath("~/logs/"+DateTime.Now.ToString("yyyy-MM-dd")+"/");
if(!Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
path = path + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
System.IO.StreamWriter sw = System.IO.File.AppendText(path);
sw.Write("订单没有会员号");
sw.Close();
为什么在写".log"这样的文件时,会这样,写".txt"文件时,第一种方法却没问题,请教各位帮帮忙,谢谢...
string path = HttpContext.Current.Server.MapPath("~/logs/"+DateTime.Now.ToString("yyyy-MM-dd")+"/");
if(!Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
path = path + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
System.IO.FileStream fs = new System.IO.FileStream(path,System.IO.FileMode.Append);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
sw.Write("订单没有会员号");
sw.Close();
fs.Close();
若只用StreamWriter= System.IO.File.AppendText(path)这个方法去写,则可以正确执行,代码如下:
string path = HttpContext.Current.Server.MapPath("~/logs/"+DateTime.Now.ToString("yyyy-MM-dd")+"/");
if(!Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
path = path + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
System.IO.StreamWriter sw = System.IO.File.AppendText(path);
sw.Write("订单没有会员号");
sw.Close();
为什么在写".log"这样的文件时,会这样,写".txt"文件时,第一种方法却没问题,请教各位帮帮忙,谢谢...
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货