当只知道文件名时,如何获取文件名的路径?

时间:2017-01-30 17:54:26

标签: c#

假设我在系统C#的目录中有一个已编译的example2.exe程序(我们称之为$PATH),因此我可以从Windows的任何文件夹中调用它。在命令行中,我进入c:\apps并以现有文件作为参数运行example2.exe,例如:example2.exe file.txt。是否有可能获得file.txt的完整路径?

另一个选项可能是..\file.txt,它不会指向当前字典中的文件。

1 个答案:

答案 0 :(得分:0)

Directory.GetCurrentDirectory正是您要找的。

var fileName = "file.txt"; //comes from args obviously
var fullPath = Path.Combine(Directory.GetCurrentDirectory(), fileName);