20

How do I get the full path for a given file?

e.g. I provide:

string filename = @"test.txt"; 

Result should be:

Full File Path = C:\Windows\ABC\Test\test.txt 
2
  • Do you want to search the test.txt file on your drive and return its path or you do you want to attach some default path to the file ? Commented Nov 1, 2012 at 11:17
  • Since between the flag time and the changes done, here is the update link with exact duplicate stackoverflow.com/questions/714101/… Commented Nov 1, 2012 at 11:20

9 Answers 9

18

Try

string fileName = "test.txt"; FileInfo f = new FileInfo(fileName); string fullname = f.FullName; 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Thanks for reply. It is returning current directory, But i required from all directories.
12

Use Path.GetFullPath():

http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx

This should return the full path information.

Comments

8

You Can use:

string path = Path.GetFullPath(FileName); 

it will return the Full path of that mentioned file.

Comments

7

Directory.GetCurrentDirectory

string dirpath = Directory.GetCurrentDirectory(); 

Prepend this dirpath to the filename to get the complete path.

As @Dan Puzey indicated in the comments, it would be better to use Path.Combine

Path.Combine(Directory.GetCurrentDirectory(), filename) 

1 Comment

And don't prepend it. Use Path.Combine instead.
5

I know my answer it's too late, but it might helpful to other's
Try,

Void Main() { string filename = @"test.txt"; string filePath= AppDomain.CurrentDomain.BaseDirectory + filename ; Console.WriteLine(filePath); } 

1 Comment

Per the Microsoft Docs, the BaseDirectory is used by "the assembly resolver to probe for assemblies." It resolves to the executable path (not the working directory) such as "bin/Debug/...". Therefore, it will only solve the OP's question in certain edge cases where the working directory and executable directory are the same. In most other cases, the executable directory will not be accessible by the current user.
3

try..

Server.MapPath(FileUpload1.FileName); 

4 Comments

If I understand the question, MapPath is indeed the way to go msdn.microsoft.com/en-us/library/…
HttpContext won't be available in WinForms
Server.MapPath is for IIS/web based applications. The tag on the question is windows
i required for windows forms.
3

try:

string fileName = @"test.txt"; string currentDirectory = Directory.GetCurrentDirectory(); string[] fullFilePath = Directory.GetFiles(currentDirectory, filename, SearchOption.AllDirectories); 

it will return full path of all such files in the current directory and its sub directories to string array fullFilePath. If only one file exist it will be in "fullFileName[0]".

Comments

1
private const string BulkSetPriceFile = "test.txt"; ... var fullname = Path.GetFullPath(BulkSetPriceFile); 

Comments

0

You can get the current path:

string AssemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString(); 

Good luck!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.