Seems it's not easy, I don't want to create a bat call an exe to do that.
Is 开发者_如何学Gothere a method to create a windows exe, that can change the current directory when it exit.
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?
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.
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. :-)
精彩评论