1 c# indexing filenames default path
在我试图进一步面向未来的项目时,我试图找到使用C#检索Web目录中索引/默认页面的完整路径和文件名的最佳方法,而不知道Web服务器的文件名列表可能性.
'Server.MapPath("/ test /")'给我'C:\ www\test \'
...这样做:'Server.MapPath(Page.ResolveUrl("/ test /"))'
...但我需要'C:\ www\test\index.html'.
有人知道现有的检索文件名的方法,当有人浏览到该目录时,网络服务器将提供该文件名 - 无论是default.aspx,还是index.html,还是其他什么?
感谢任何帮助,饲料
ASP.NET不知道这一点.您需要在IIS中查询默认文档列表.
原因是IIS将在您的Web文件夹中查找IIS默认文档列表中的第一个匹配文件,然后在脚本映射中切换到该文件类型的匹配ISAPI扩展(通过扩展名).
要获取默认文档列表,您可以执行以下操作(使用默认网站作为IIS编号= 1的示例):
using System;
using System.DirectoryServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (DirectoryEntry w3svc =
new DirectoryEntry("IIS://Localhost/W3SVC/1/root"))
{
string[] defaultDocs =
w3svc.Properties["DefaultDoc"].Value.ToString().Split(',');
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,这将是迭代defaultDocs
数组以查看文件夹中存在哪个文件的情况,第一个匹配是默认文档.例如:
// Call me using: string doc = GetDefaultDocument("/");
public string GetDefaultDocument(string serverPath)
{
using (DirectoryEntry w3svc =
new DirectoryEntry("IIS://Localhost/W3SVC/1/root"))
{
string[] defaultDocs =
w3svc.Properties["DefaultDoc"].Value.ToString().Split(',');
string path = Server.MapPath(serverPath);
foreach (string docName in defaultDocs)
{
if(File.Exists(Path.Combine(path, docName)))
{
Console.WriteLine("Default Doc is: " + docName);
return docName;
}
}
// No matching default document found
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
遗憾的是,如果您处于部分信任的ASP.NET环境(例如共享托管)中,这将无法工作.
归档时间: |
|
查看次数: |
3175 次 |
最近记录: |