Moriya Samun Moriya Samun - 1 year ago 206
C# Question

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

I am trying to get the full path a file by its name only.
I have tried to use :

string fullPath = Path.GetFullPath("excelTest");

but it returns me an incorrect path (something with my project path).

I have read somewhere here a comment which says to do the following:

var dir = Environment.SpecialFolder.ProgramFilesX86;
var path = Path.Combine(dir.ToString(), "excelTest.csv");

but I do not know where the file is saved , therefore I do not know its environment.

can someone help me how to get the full path of a file only by its name?

Answer Source

I guess you're trying to find file (like in windows search), right ?

I'd 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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download