1

I have relative path of a directory like "..\work\FilesDirectory". How to get all files from this directory. I am currently using the following line of code but it requiresw absolute path.

string []AllFiles = Directory.GetFiles(sPath);
Ahsan
  • 648
  • 1
  • 10
  • 27
  • Possible duplicate : http://stackoverflow.com/questions/3259583/how-to-get-files-in-a-relative-path-in-c-sharp?rq=1 – Jonas W Jan 28 '14 at 06:20

3 Answers3

3

You should you Path.Combine to construct absolute path or you can change Current Directory so relative path points to location you need.

// Building c:\my\home\work\FilesDirectory
var absolutePath = Path.Combine(@"c:\my\home\toys\", @"..\work\FilesDirectory" );

Note: you need to know what location the path is relative to. I.e. if it relative to executable use Assembly.GetEntryAssembly().Location as base path.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
2

If relative path represents your Project Debug folder then you can use:

string relativePath = AppDomain.CurrentDomain.BaseDirectory & "work\FilesDirectory"

Or you can also use config file to save your path and use string.Join() but its better to use Path.Combine instead.

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
1

You could always use the Directory.GetCurrentDirectory and the Directory.GetParent methods.

DirectoryInfo parentDirectoryInfo = System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory());
string []AllFiles = Directory.GetFiles(parentDirectoryInfo.FullName);

Hope I helped!

Pantelis Natsiavas
  • 5,293
  • 5
  • 21
  • 36