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 Try
string fileName = "test.txt"; FileInfo f = new FileInfo(fileName); string fullname = f.FullName; Use Path.GetFullPath():
http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
This should return the full path information.
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) Path.Combine instead.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); } try..
Server.MapPath(FileUpload1.FileName); MapPath is indeed the way to go msdn.microsoft.com/en-us/library/…HttpContext won't be available in WinFormstry:
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]".
test.txtfile on your drive and return its path or you do you want to attach some default path to the file ?