2010-06-18 91 views
1

我试图设置一个页面,允许用户从共享驱动器(其中该文件实际上是通过该页面发送的)下载文件。到目前为止,这是我的。System.InvalidOperationException:无法映射'/ sharedDrive/Public'路径

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身份验证我的用户(其可以访问驱动器)。我不明白这个问题会是什么,甚至是抱怨什么。

回答

2

您不应该为MapInfo调用UNC文件路径。 Server.MapPath()为指定相对于ASP.NET应用程序根目录的路径的文件构建完整的本地路径。

例如:

Server.MapPath(@"MyRelativeDir\MyRelativePath.txt") 

可能返回C:\ myiisappdir \ MyRelativeDir \ MyRelativePath.txt

下面的代码是非法的,因为远程路径相对于应用程序根不是:

Server.MapPath(@"\\sharedDrive\Public") 

所以如果你的IIS应用程序的身份和共享权限是正确的,我会认为下面的守ld工作:

DirectoryInfo info = new DirectoryInfo(@"\\sharedDrive\Public"); 
+0

很好的解释。我很确定它现在工作正常:) – 2010-06-18 16:08:32