如果未指定命令行参数,则使用命令行的目录
在 C# 控制台应用程序中,我打算获取适当的目录。例如,假设我有以下代码:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(args[0]);
}
}
并且我在命令提示符下执行此操作:
FooCA.exe .\Foo\Bar
我得到的是完全相同的字符串。那么,如何将其转换为完整路径呢?
如果没有指定命令行参数,我想获取命令行的目录。
In c# console application, I intend to get the appropriate directory. For example, let's assume that I have the following code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(args[0]);
}
}
and I do this inside the command prompt:
FooCA.exe .\Foo\Bar
What I get is exactly the same string. So, how can I convert this to a full path?
And I would like to get the directory of the command line if no Command-Line Arguments specified.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 System.IO.Path.GetFullPath() 解析相对目录,并使用 System.IO.Directory.GetCurrentDirectory() 查找当前工作目录(即正如你所说,命令行的目录)
You can use
System.IO.Path.GetFullPath()
to resolve relative directories andSystem.IO.Directory.GetCurrentDirectory()
to find the current working directory (ie, the directory of the command line, as you say)System.IO.Path.GetFullPath(path)
:返回指定路径字符串的绝对路径..Environment.CurrentDirectory
:获取或设置当前工作目录的完全限定路径。System.IO.Path.GetFullPath(path)
: Returns the absolute path for the specified path string..Environment.CurrentDirectory
: Gets or sets the fully qualified path of the current working directory.您可以使用:
这将考虑当前执行目录。从用户输入中获取路径时要小心验证路径,因为它们可能是恶意的。
You can use:
This will take into account the current executing directory. Be careful to validate your paths when taking them from user input as they could be malicious.