c#中如何访问虚拟目录路径?

Posted

技术标签:

【中文标题】c#中如何访问虚拟目录路径?【英文标题】:How to access the virtual directory path in c#? 【发布时间】:2021-05-16 06:46:40 【问题描述】:

我正在处理工作人员服务,并在 IIS 服务器内创建了两个虚拟目录。目标是将文件从一个文件夹移动到另一个文件夹。文件移动部分工作正常,只是我需要提供源和目标目录路径。

我在wwwroot 中放置了两个文件夹,名称分别为SourceDestination。我查看了博客和谷歌,但找不到任何扩展。

任何提示都会很明显。谢谢

【问题讨论】:

这能回答你的问题吗? How to get virtual directory physical path 不是陛下。我可以用这个访问Server.MapPath("~/Images") 吗? 【参考方案1】:

您可以尝试使用Microsoft.Web.Administration dll 来获取虚拟目录路径。可以参考以下代码。

ServerManager serverManager = new ServerManager();
// get the site (e.g. default)
Site site = serverManager.Sites.FirstOrDefault(s => s.Name == "Default Web Site");
// get the application that you are interested in
Application myApp = site.Applications["/Dev1"];
// get the physical path of the virtual directory
Console.WriteLine(myApp.VirtualDirectories[0].PhysicalPath);

【讨论】:

我试过了,但它给了我一个异常。顺便说一句,这是一个工作服务,而不是 Web 应用程序 我试过了,但它给了我一个异常。 system unauthorizedaccessexception。如何授予应用程序权限 您需要以管理员身份运行visual studio,并确保IIS_IUSRS对wwwroot文件夹有读写权限。 正如我所说,我在 IIS 上运行它,而不是从 Visual Studio 运行。 你的标题说获取C#中的虚拟目录路径,如果你在iis中,你只需要双击你的网站就可以查看虚拟目录。查看它的属性,你就会知道它的路径。

以上是关于c#中如何访问虚拟目录路径?的主要内容,如果未能解决你的问题,请参考以下文章

如何在应用程序中提供虚拟目录路径

如何把物理路径转换成虚拟路径

如何设置nginx的虚拟域名及访问目录

tomcat的虚拟路径问题

C#中网站根路径、应用根路径、物理路径、绝对路径,虚拟路径的区别

IIS虚拟目录与UNC路径权限初探