Ranjeeth Kumar Pathi
Ranjeeth Kumar Pathi

Reputation: 227

How to get full file path from file name?

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

Answers (9)

David Castro
David Castro

Reputation: 1957

You can get the current path:

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

Good luck!

Upvotes: 0

Aishu
Aishu

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

Akhil Achuthan
Akhil Achuthan

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

sanjoy
sanjoy

Reputation: 81

You Can use:

string path = Path.GetFullPath(FileName);

it will return the Full path of that mentioned file.

Upvotes: 8

Yaugen Vlasau
Yaugen Vlasau

Reputation: 2218

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

Upvotes: 1

rohan panchal
rohan panchal

Reputation: 881

try..

Server.MapPath(FileUpload1.FileName);

Upvotes: 3

Matthew Watson
Matthew Watson

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

Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46728

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)

Upvotes: 7

codingbiz
codingbiz

Reputation: 26376

Try

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

Upvotes: 18

Related Questions