C# System.InvalidOperationException:未能映射路径'/sharedDrive/Public';
我正在尝试设置一个页面,允许用户从共享驱动器下载文件(文件实际上是通过页面发送的)。到目前为止,这就是我所拥有的C# System.InvalidOperationException:未能映射路径'/sharedDrive/Public';,c#,asp.net,file,C#,Asp.net,File,我正在尝试设置一个页面,允许用户从共享驱动器下载文件(文件实际上是通过页面发送的)。到目前为止,这就是我所拥有的 public partial class TestPage : System.Web.UI.Page { protected DirectoryInfo dir; protected FileInfo[] files; protected void Page_Load(object sender, EventArgs e) { dir
public partial class TestPage : System.Web.UI.Page
{
protected DirectoryInfo dir;
protected FileInfo[] files;
protected void Page_Load(object sender, EventArgs e)
{
dir = new DirectoryInfo(Server.MapPath(@"\\sharedDrive\Public"));
files = dir.GetFiles();
}
}
aspx页面看起来有点像这样:
<% Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name); %>
<ul>
<% foreach (System.IO.FileInfo f in files)
{
Response.Write("<li>" + f.FullName + "</li>");
} %>
</ul>
当我删除代码的错误部分时,页面会告诉我,我使用的Windows标识是我的用户(可以访问驱动器)。我不明白问题可能是什么,甚至不知道它在抱怨什么。您不应该为UNC文件路径调用MapPathServer.MapPath()在给定相对于ASP.NET应用程序根目录的路径的情况下,为文件构建完整的本地路径 例如:
Server.MapPath(@"MyRelativeDir\MyRelativePath.txt")
可能返回c:\myiisappdir\MyRelativeDir\MyRelativePath.txt
以下代码是非法的,因为远程路径与应用程序根不相关:
Server.MapPath(@"\\sharedDrive\Public")
因此,如果您的IIS应用程序的标识和共享权限正确,我认为以下方法应该可以工作:
DirectoryInfo info = new DirectoryInfo(@"\\sharedDrive\Public");
很好的解释。我很确定它现在工作正常:)