Reputation: 227
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
Upvotes: 20
Views: 171084
Reputation: 1957
You can get the current path:
string AssemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();
Good luck!
Upvotes: 0
Reputation: 1320
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);
}
Upvotes: 5
Reputation: 53
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]".
Upvotes: 3
Reputation: 81
You Can use:
string path = Path.GetFullPath(FileName);
it will return the Full path of that mentioned file.
Upvotes: 8
Reputation: 2218
private const string BulkSetPriceFile = "test.txt";
...
var fullname = Path.GetFullPath(BulkSetPriceFile);
Upvotes: 1
Reputation: 109547
Use Path.GetFullPath():
http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
This should return the full path information.
Upvotes: 12
Reputation: 46728
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)
Upvotes: 7
Reputation: 26376
Try
string fileName = "test.txt";
FileInfo f = new FileInfo(fileName);
string fullname = f.FullName;
Upvotes: 18