如何从文件名获取完整的文件路径?

Ran*_*thi 7 c# winforms

如何获取给定文件的完整路径?

例如,我提供:

string filename = @"test.txt";
Run Code Online (Sandbox Code Playgroud)

结果应该是:

Full File Path = C:\Windows\ABC\Test\test.txt
Run Code Online (Sandbox Code Playgroud)

cod*_*biz 13

尝试

string fileName = "test.txt";
FileInfo f = new FileInfo(fileName);
string fullname = f.FullName;
Run Code Online (Sandbox Code Playgroud)


小智 8

您可以使用:

string path = Path.GetFullPath(FileName);
Run Code Online (Sandbox Code Playgroud)

它将返回上述文件的完整路径。


Ani*_*han 6

Directory.GetCurrentDirectory

string dirpath = Directory.GetCurrentDirectory();
Run Code Online (Sandbox Code Playgroud)

前面加上这个dirpath到文件名,以获得完整路径.

正如@Dan Puzey在评论中指出的那样,最好使用Path.Combine

Path.Combine(Directory.GetCurrentDirectory(), filename)
Run Code Online (Sandbox Code Playgroud)

  • 并且不要在它之前.请改用"Path.Combine". (2认同)

Mat*_*son 6

使用Path.GetFullPath():

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

这应该返回完整的路径信息.


Ais*_*shu 5

我知道我的回答为时已晚,但可能对其他人有帮助
尝试,

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


roh*_*hal 3

尝试..

Server.MapPath(FileUpload1.FileName);
Run Code Online (Sandbox Code Playgroud)