Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Directory.Getfiles with specifing the absolute path

Tags:

c#

asp.net

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

like image 629
SxChoc Avatar asked Feb 20 '23 04:02

SxChoc


1 Answers

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.

like image 184
Adriano Repetti Avatar answered Mar 05 '23 04:03

Adriano Repetti