代码之家  ›  专栏  ›  技术社区  ›  Joe Phillips

System.InvalidOperationException:未能映射路径“/sharedDrive/Public”

  •  1
  • Joe Phillips  · 技术社区  · 15 年前

    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>
    

    1 回复  |  直到 15 年前
        1
  •  2
  •   Joe Phillips    15 年前

    不必为UNC文件路径调用MapPath。 服务器.MapPath() 为给定相对于ASP.NET应用程序根目录的路径的文件生成完整的本地路径。

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

    可能会回来 c:\myiisappdir\MyRelativeDir\MyRelativePath.txt文件

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

    因此,如果您的IIS应用程序的标识和共享权限是正确的,我认为以下应该可以工作:

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