在Windows命令环境中更改当前目录

发布于 2024-08-09 08:44:36 字数 90 浏览 11 评论 0原文

似乎这并不容易,我不想创建一个调用 exe 的 bat 来做到这一点。

有没有一种方法可以创建一个Windows exe,它可以在退出时更改当前目录。

Seems it's not easy, I don't want to create a bat call an exe to do that.

Is there a method to create a windows exe, that can change the current directory when it exit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-08-16 08:44:36

简短的回答是否定的。
操作系统为每个可执行文件创建“环境”的副本。这包括当前工作目录、“PATH”等环境变量列表等。如果您的可执行文件更改了当前工作目录,则此更改只会影响程序运行时自己的环境,但不会影响父进程就这样开始了。

由于每个进程都有自己的“当前目录”,那么“更改当前目录”是什么意思?当前目录是什么?

The short answer is no.
The operating system creates a copy of the "environment" for every executable. This includes things like the current working directory, a list of environment variables like "PATH", etc. If your executable changes it's current working directory, this change will only affect the program's own environment while it runs, but will not affect the parent process that started it.

Since every process has its own "current directory", what do you mean by "change the current directory"? The current directory for what?

梓梦 2024-08-16 08:44:36

TakeCommand 的 Windows 4DOS 扩展命令 shell 允许您执行此操作。但在基本的.bat 中?我希望你必须编写一个命令行 win32 exe 来为你提供你想要的东西?一定还有其他的 shell 替代品你可以看看 - .bat 和 Windows 命令行 shell 是令人难以置信的大脑损伤/脆弱。

TakeCommand's 4DOS extended command shell for windows allows you to do this. But in basic .bat? I would expect you'd have to write a command-line win32 exe that provided you with what you want? There must be other shell-replacements you could look at - .bat and the Windows command-line shell are incredibly brain-damaged / weak.

烟雨扶苏 2024-08-16 08:44:36

当然还有 SetCurrentDirectory Win32 API 调用。但是,这只会更改调用进程的工作目录。由于每次运行可执行文件时,操作系统都会为其启动一个新进程,因此没有一个好的方法可以做到这一点。

有一个SO问题关于获取此信息。也许里面的东西会对你有帮助。接受的答案看起来可能会让您直接访问它,但看起来也非常危险。如果你的系统蓝屏或者引发了中国综合症之类的,那是你的错,不是我的。 :-)

Well there is of course the SetCurrentDirectory Win32 API call. However, that only changes the working directory for the calling process. Since each time you run an executable the OS starts up a new process for it, there isn't going to be a good way to do that.

There was a SO question about getting this information. Perhaps something in there will help you. The accepted answer looks like it might get you direct access to it, but is also looks really dangerous. If you bluescreen your system or set off a China Sindrome or something, it was your fault, not mine. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文