kin*_*005 9 .net c# programming-languages
我有一个命令行程序,它将配置文件作为参数,即C:\ myprogram.exe -c C:\ configFiles\MyConfigFile.txt.我希望能够这样做,这样我就不必输入配置文件的绝对路径,所以我可以输入C:\ myprogram.exe -c MyConfigFile.txt而不是上面的命令.
我不太熟悉C#如何处理路径以了解是否或如何做到这一点.任何见解都表示赞赏.
Nem*_*157 13
就像是:
string path = "MyConfigFile.txt";
string fullPath = System.IO.Path.GetFullPath(path);
Run Code Online (Sandbox Code Playgroud)
如果配置文件在当前目录中.如果它们存储在标准文件夹中,您可以使用Path.Combine()
string basePath = "C:\Configs";
string file = "MyConfigFile.txt";
string fullPath = System.IO.Path.Combine(basePath, file);
Run Code Online (Sandbox Code Playgroud)
你只是传入一个字符串。由您以相对路径的方式处理它。
如果您使用此代码,默认情况下它将支持相对路径。
FileInfoEnvironment.CurrentDirectory如果提供的路径不明确,将首先尝试使用路径。
var file = new FileInfo(args[1]); // args[1] can be a filename of a local file
Console.WriteLine(file.FullName); // Would show the full path to the file
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24918 次 |
| 最近记录: |