控制台应用程序中的命令行当前工作目录 [英] Command line current working directory in a console app

查看:103
本文介绍了控制台应用程序中的命令行当前工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何获取运行程序的控制台的当前工作目录,以便我可以解析作为程序args传递的相对路径?

How to get current working directory of a console that runs a program so I could resolve relative paths passed as program args?

让我说我的程序放在这里: c:\tools\program.exe
但是我从各个地方调用它。可以说我在这里: C:\Users\Me\Documents\ ,然后运行此命令 program.exe --src = somefile.txt --dest = subdir\otherfile.txt

Lets say I've put my program here: c:\tools\program.exe But I'm invoking it from various places. Lets say I'm here: C:\Users\Me\Documents\ and I run this command program.exe --src=somefile.txt --dest=subdir\otherfile.txt

Environment.CurrentDirectory System.Reflection.Assembly.GetExecutingAssembly()。Location 将返回 c:\tools ,但我会希望能够解析相对于<$ c的 somefile.txt subdir\oterfile.txt 路径$ c> C:\Users\Me\Documents\ 。

Environment.CurrentDirectory and System.Reflection.Assembly.GetExecutingAssembly().Location will return c:\tools but I would like to be able to resolve somefile.txt and subdir\oterfile.txt paths that are relative to C:\Users\Me\Documents\.


==== ==更新======

====== UPDATE ======

感谢您的帮助。看来 Environment.CurrentDirectory 可以正常工作。事实证明,在我的情况下,问题是由Xenocode的Postbuild工具(现在称为Spoon Virtual Application Studio)引起的,我有时使用该工具将所有程序的文件(包括dll,config等)打包成一个可执行文件。这非常方便,但是在这种情况下,虚拟化功能弄乱了我程序的 Environment 变量。我已经设法解决了这个问题。

Thank you for your help. It seems that Environment.CurrentDirectory works as expected. It turned out that in my case the problem was caused by Xenocode's Postbuild tool (now called Spoon Virtual Application Studio) that I occasionally use to "pack" all program's files (including dlls, config, etc.) into one executable. It's very handy, but in this case the "virtualization" feature messed up my program's Environment variables. I've managed to solve that issue.


推荐答案

Environment.CurrentDirectory为您提供当前的工作目录

Environment.CurrentDirectory gives you the Current Working Directory

让我展示一个简单的示例:

Let me show a simple example:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Startup: " + Environment.CurrentDirectory);
        Environment.CurrentDirectory = @"D:\temp";
        Console.WriteLine("After:" + Environment.CurrentDirectory);
    }
}

现在,我创建两个名为 D:\temp 和 D:\temp2

我将可执行文件放入 D:\temp

打开命令提示符,并使用 cd设置当前工作目录D:\temp2 < br>
从该目录运行 .. \temp\mytestapp.exe

Now I create two folders named D:\temp and D:\temp2
I put the executable in D:\temp
Open a command prompt and set the current working directory with cd D:\temp2
From that directory I run ..\temp\mytestapp.exe

输出是

Startup: D:\temp2
After: D:\temp

出于好奇:

这是 Environment.CurrentDirectory 在Net 1.1中

this was the documentation for Environment.CurrentDirectory in Net 1.1


获取并设置当前目录的标准路径;
是此过程开始的目录。

Gets and sets the fully qualified path of the current directory; that is, the directory from which this process starts.

这是NET 4.0中的文档

and this is the documentation in NET 4.0


获取或设置当前工作的
目录的完全限定路径。

Gets or sets the fully qualified path of the current working directory.

这篇关于控制台应用程序中的命令行当前工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆