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);
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);
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.
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.
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!