How to get a file full path in c# with Path.GetFullPath

How to get a file full path in c# with Path.GetFullPath

The first snippet (with Path.GetFullPath) does exactly what you want. It returns something with your project path because the program EXE file is located in the projectBinDebug path, which is therefore the current directory.

If you want to search for a file on a drive, you can use Directory.GetFiles, which will recursively search for a file in a directory given a name pattern.

This returns all xml-files recursively :

var allFiles = Directory.GetFiles(path, *.xml, SearchOption.AllDirectories);

https://stackoverflow.com/a/9830162/2196124

I guess youre trying to find file (like in windows search), right ?

Id look into this question – you will find all files that has that string in their filename, and from there you can return full filepath.

var fileList = new DirectoryInfo(@c:).GetFiles(*excelTest*, SearchOption.AllDirectories);

And then just use foreach to do you manipulations, e.g.

foreach(string file in fileList)
{
// MessageBox.Show(file);
}

How to get a file full path in c# with Path.GetFullPath

What youre looking for is Directory.GetFiles(), you can read up on it here. The gist of it is, youll pass in the file path and the file name, and youll get a string array back. In this instance, you can assume top level with C:. It should be noted, that if nothing is found, the string array will be empty.

Leave a Reply

Your email address will not be published.