简体   繁体   English

我需要澄清IIS中的虚拟目录

[英]I need some clarification on Virtual directory in IIS

OK. 好。 Maybe I am not doing this right but I can tell you what I am doing and maybe someone can help. 也许我做得不对,但我可以告诉你我在做什么,也许有人可以提供帮助。

I have my application located in http:\\localhost\\MyApplication ( c:\\inetpub\\wwwroot\\MyApplication). 我的应用程序位于http:\\ localhost \\ MyApplication(c:\\ inetpub \\ wwwroot \\ MyApplication)中。 Now I have a virtual directory created in http:\\localhost\\MyApplication\\ImagePaths ( pointing to d:\\images folder) 现在我在http:\\ localhost \\ MyApplication \\ ImagePaths中创建了一个虚拟目录(指向d:\\ images文件夹)

within one of my aspx pages I am trying to list all the images in http:\\localhost\\MyApplication\\ImagePaths virtual Directory. 在我的一个aspx页面中,我试图列出http:\\ localhost \\ MyApplication \\ ImagePaths虚拟目录中的所有图像。
I have tried: DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/ImagePaths")); 我试过:DirectoryInfo dir = new DirectoryInfo(Server.MapPath(“〜/ ImagePaths”)); Dir contains c:\\inetpub\\wwwroot\\images !!??!?!? Dir包含c:\\ inetpub \\ wwwroot \\ images !! ??!?!?

What is the right way of doing this? 这样做的正确方法是什么? I just want to create a directory outside of my application and list the content within my application. 我只想在我的应用程序之外创建一个目录,并在我的应用程序中列出内容。

Thanks 谢谢

I would use <appSettings> section of the web.config and store it there: 我会使用web.config的<appSettings>部分并将其存储在那里:

<appSettings>
   <add key="ImagesPath" value="D:\Images"/>
</appSettings>

Then from code, you can access it via: 然后从代码中,您可以通过以下方式访问它:

string imagesPath = ConfigurationManager.AppSettings["ImagesPath"];

and: 和:

DirectoryInfo dir = new DirectoryInfo(imagesPath);

The ~ means "from the root of my virtual directory". 〜表示“从我的虚拟目录的根目录”。 You're then building a path from there, that doesn't exist, and asking for the location of it on disk. 然后,您将从那里构建一条不存在的路径,并询问它在磁盘上的位置。

You'll get an answer to that even if the folder doesn't exist. 即使文件夹不存在,您也会得到答案。

Why not use configuration to state where the images can be found, rather than hard coding a virtual directory? 为什么不使用配置来说明可以找到图像的位置,而不是硬编码虚拟目录? Your current approach would his issues as soon as you host those vdirs on separate machines anyway, so it's better to use config to store the unc path. 一旦你在不同的机器上托管这些vdir,你当前的方法就会出现问题,因此最好使用config来存储unc路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM