SxChoc
SxChoc

Reputation: 619

Using Directory.Getfiles with specifing the absolute path

Hi I wonder if you guys could help please.

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....

The folder structure is

\controls\ ---- This contains the aspx file that is going to check if an xml file exists in the blogengine folder.

\blogengine\ This should contain xml files

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?

Does anyone know??

Thanks in advance, Craig

Upvotes: 4

Views: 7855

Answers (3)

dotINSolution
dotINSolution

Reputation: 234

Use Path.GetFullPath to get the absolute path from your relative path.

So, your code becomes:

string[] filePaths = Directory.GetFiles(Path.GetFullPath(@"..\BlogEngine\"), "*.xml");

Upvotes: 1

Adriano Repetti
Adriano Repetti

Reputation: 67148

Use the Server.MapPath() method to map a relative path (based on current directory or web-site root) to an absolute accessible path.

For example:

string folderPath = Server.MapPath("~/BlogEngine");
string[] filePaths = Directory.GetFiles(folderPath, "*.xml");

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.

Upvotes: 7

SLaks
SLaks

Reputation: 888213

In an ASP.Net application, you can call Server.MapPath("~/path") to resolve a path on disk relative to the application root directory.

Upvotes: 1

Related Questions