如何获取给定文件的完整路径?
例如,我提供:
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)
它将返回上述文件的完整路径。
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.GetFullPath():
http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
这应该返回完整的路径信息.
我知道我的回答为时已晚,但可能对其他人有帮助
尝试,
Void Main()
{
string filename = @"test.txt";
string filePath= AppDomain.CurrentDomain.BaseDirectory + filename ;
Console.WriteLine(filePath);
}
Run Code Online (Sandbox Code Playgroud)