使用Directory.Getfiles与specifing绝对路径 [英] Using Directory.Getfiles with specifing the absolute path
问题描述
您好我想知道,如果你们能帮助吧。
Hi I wonder if you guys could help please.
我正在写想要住在多个服务器上,因此多个IP的和,而不是使用确切的IP和目录结构,我想只是从应用程序实际运行的地方退一步的文件夹的应用程序。因此,例如......
I'm writing a application that is going to live on multiple servers and therefore multiple IP's and instead of using the exact IP and directory structure I'd like to just 'step back' a folder from where the application is actually running. So for instance....
该文件夹结构
\\控制\\ ----这包含了将要检查一个XML文件中的文件夹blogengine存在aspx文件。
\controls\ ---- This contains the aspx file that is going to check if an xml file exists in the blogengine folder.
\\ blogengine \\这应该包含XML文件
\blogengine\ This should contain xml files
在code,我使用是......
The code that I'm using is......
string[] filePaths = Directory.GetFiles(@"\\94.175.237.35\inetpub$\wwwroot\mytest\BlogEngine\", "*.xml");
这将找到特定的服务器上的文件,但我需要找到的文件相对于应用程序上运行,这样我就不必不断变化的文件路径的服务器。所以我需要的东西像
Which will find the files on a particular server but I need to find the files relative to the server that the application is running on, so that I don't have to keep changing the file path. So I'd need something like
string[] filePaths = Directory.GetFiles(@"..\BlogEngine\", "*.xml");
但是,为了做到这一点我无法找到正确的语法?
But I can't find the correct syntax in order to do this?
有谁知道?
在此先感谢,
克雷格
Thanks in advance, Craig
推荐答案
使用使用Server.Mappath()
(基于当前目录或Web方法映射的相对路径上门根),以绝对访问路径。
Use the Server.MapPath()
method to map a relative path (based on current directory or web-site root) to an absolute accessible path.
例如:
string folderPath = Server.MapPath("~/BlogEngine");
string[] filePaths = Directory.GetFiles(folderPath, "*.xml");
波浪符号重新presents你的网站的根目录下,如果你指定相对路径,然后它会被解析为相对于您的ASP.NET页面所在的目录。
The tilde character represents the root of your web-site, if you specify a relative path then it'll be resolved as relative to the directory where your ASP.NET page resides.
这篇关于使用Directory.Getfiles与specifing绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!