WebServer中使用FileStream流读写IIS的虚拟目录中的文件报错. 从服务器中下文件,使用的方式是在WebServer中使用FileStream流来读写文件, 但是读到IIS的虚拟目录中的文件就会报出的错是"访问被拒绝", 是权限的原因还是其他原因?有人知道怎么解决吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在文件修改权限中添加ASPNET用户为完全控制 对文件所存的目录具有可写权限即可不过你还要注意一个问题,参看http://blog.csdn.net/knight94/archive/2006/03/31/646252.aspx 我从其他路径中读写文件都没问题, 惟独IIS目录, 可昨天晚上又试了下.能往IIS里写文件,就是读文件的时候报错. FileStream fs = new FileStream(Server.MapPath(".") + @"\aaa.txt", FileMode.Open);程序运行到这段代码就报错走不过去, 这段代码写在WebService中,我用WinForm调用. {"System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.UnauthorizedAccessException: 对路径“c:\\inetpub\\wwwroot\\WebSite\\aaa.txt”的访问被拒绝。\n 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)\n 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)\n 在 System.IO.FileStream..ctor(String path, FileMode mode)\n 在 Service.bt()\n --- 内部异常堆栈跟踪的结尾 ---"} System.Exception {System.Web.Services.Protocols.SoapException}这是程序报的错. to FileStream fs = new FileStream(Server.MapPath(".") + @"\aaa.txt", FileMode.Open);你看看Server.MapPath(".")获得是什么 得到的是webservice程序的目录路径"c:\inetpub\wwwroot\WebSite" 下面的代码是webservice里读文件的函数接口.[WebMethod] public byte[] bt() { byte[] bt = null; FileStream fs = new FileStream(Server.MapPath(".") + @"\aaa.txt",FileMode.Open); //FileStream fs = new FileStream(@"d:\test.txt", FileMode.Open); bt = new byte[fs.Length]; fs.Read(bt, 0, bt.Length); fs.Close(); return bt; } changeFileStream fs = new FileStream(Server.MapPath(".") + @"\aaa.txt",FileMode.Open);withFileStream fs = new FileStream(Server.MapPath(".") + @"\aaa.txt",FileMode.Open,FileAccess.Read); 你最好查看一下aspnet用户是否对此目录具有访问权限。 在家的机子上通过了,用FileStream fs = new FileStream(Server.MapPath(".") + @"\aaa.txt",FileMode.Open,FileAccess.Read);替换了原来的FileStream fs = new FileStream(Server.MapPath(".") + @"\aaa.txt",FileMode.Open);FileAccess.Read 这个参数是什么意思? 是准确的告诉系统本次操作只是对文件的读取吗? 刚试着把FileAccess.Read 改成 FileAccess.Write和FileAccess.ReadWrite 就报出权限的错误, 由此可以得出,第二个参数为FileMode.Open时,不具备写入的权限, 可为什么上传时就不会报出权限的问题呢? 我上传的代码是 [WebMethod] public void upload(byte[] bt) { MemoryStream m = new MemoryStream(bt); FileStream fs = new FileStream(Server.MapPath(".") + @"\test.txt", FileMode.Create); m.WriteTo(fs); m.Close(); fs.Close(); }这段代码却能正常的通过,为什么? 新人问个泛型问题 来牛人啊!求解,将十进制转换为BCD码! 在C#中怎样将Unicode的字符编码转换成gb2312字符编码啊 新手,问个关于接口的问题 如何在父窗体中显示子窗体 提问:关于try finally 大大们帮忙改下程序吧,我新手,错了尽管说 如何立即释放系统资源?up有分 ACCESS2003如何获取表的当前自增ID 学习C# 入门问题,过程如何写,谢谢指导 如何写这样的代码 大哥们救命,IDictionary的问题。
http://blog.csdn.net/knight94/archive/2006/03/31/646252.aspx
程序运行到这段代码就报错走不过去, 这段代码写在WebService中,我用WinForm调用.
这是程序报的错.
[WebMethod]
public byte[] bt()
{
byte[] bt = null;
FileStream fs = new FileStream(Server.MapPath(".") + @"\aaa.txt",FileMode.Open);
//FileStream fs = new FileStream(@"d:\test.txt", FileMode.Open);
bt = new byte[fs.Length];
fs.Read(bt, 0, bt.Length);
fs.Close();
return bt;
}
FileStream fs = new FileStream(Server.MapPath(".") + @"\aaa.txt",FileMode.Open);with
FileStream fs = new FileStream(Server.MapPath(".") + @"\aaa.txt",FileMode.Open,
FileAccess.Read);
FileStream fs = new FileStream(Server.MapPath(".") + @"\aaa.txt",FileMode.Open,
FileAccess.Read);
替换了原来的
FileStream fs = new FileStream(Server.MapPath(".") + @"\aaa.txt",FileMode.Open);FileAccess.Read 这个参数是什么意思? 是准确的告诉系统本次操作只是对文件的读取吗?
可为什么上传时就不会报出权限的问题呢? 我上传的代码是
[WebMethod]
public void upload(byte[] bt)
{
MemoryStream m = new MemoryStream(bt);
FileStream fs = new FileStream(Server.MapPath(".") + @"\test.txt", FileMode.Create);
m.WriteTo(fs);
m.Close();
fs.Close();
}
这段代码却能正常的通过,为什么?