How to get the full path to the file from the file name?

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

eg. I provide:

string filename = @"test.txt"; 

The result should be:

 Full File Path = C:\Windows\ABC\Test\test.txt 
+4
source share
9 answers

Try

 string fileName = "test.txt"; FileInfo f = new FileInfo(fileName); string fullname = f.FullName; 
+8
source

Directory.GetCurrentDirectory

 string dirpath = Directory.GetCurrentDirectory(); 

Prepare this path for the file name to get the full path.

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

 Path.Combine(Directory.GetCurrentDirectory(), filename) 
+5
source

Use Path.GetFullPath ():

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

This should return all the path information.

+5
source

to try ..

 Server.MapPath(FileUpload1.FileName); 
+2
source
 private const string BulkSetPriceFile = "test.txt"; ... var fullname = Path.GetFullPath(BulkSetPriceFile); 
+1
source

try:

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

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

+1
source

I know my answer is too late, but it can help other people. Try,

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

You can use:

 string path = Path.GetFullPath(FileName); 

it will return the full path to the specified file.

0
source

You can get the current path:

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

Good luck

0
source

Source: https://habr.com/ru/post/1443415/


All Articles