获取cmd.exe的当前工作目录
如何检索 cmd.exe 的当前工作目录?
这似乎是可能的。 例如,使用 ProcessExplorer,选择 CMD.exe,右键单击,属性,图像选项卡,“当前目录”反映使用 CD 或 CHDIR 命令设置的目录。
我查看了 .NET Process 和 ProcessStartInfo 类(ProcessStartInfo.WorkingDirectory 始终返回“”),似乎找不到确定这一点的方法。 PInvoke 也没有什么突出的地方。
作为一个例子,我希望能够以编程方式说出类似的内容: Process.GetCurrentWorkingDirectory(processID) 其中 processID 是另一个正在运行的进程的 Windows 进程 ID。
有什么解决方案吗,WinAPI 还是.NET?
[更新]
提出这个问题的原因:
我已经使用“命令提示符资源管理器栏”一段时间了,它很棒,除非我“CD”到新目录,当前资源管理器窗口也不会改变。 (即同步只是从资源管理器到命令提示符的一种方式)。 我正在寻找两种方式。
How can I retrieve the current working directory of cmd.exe?
This seems possible. For example using ProcessExplorer, select CMD.exe, right click, properties, Image tab, "Current Directory" relects the directory set using the CD or CHDIR commands.
I've looked at the .NET Process and ProcessStartInfo classes (ProcessStartInfo.WorkingDirectory always returns "") and can't seem to find a way of determining this. Nothing at PInvoke stands out either.
As an example I'm looking to programmatically be able to say something like: Process.GetCurrentWorkingDirectory(processID) where processID is a Windows process ID of another running process.
Is there any solution, WinAPI or .NET?
[Update]
Reason for asking this question:
I've used the "Command Prompt Explorer Bar" for a while and it's great except if I "CD" to a new directory, the current Explorer window does not also change. (ie the Sync is only 1 way from Explorer to the commmand prompt). I'm looking to make this 2 way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
未经测试,一种可能的方法:
创建一个带有 DllMain 的 DLL,该 DllMain 使用 GetThreadStartInformation() 查找缓冲区的地址,然后使用 GetCurrentDirectory 填充它。 这应该没问题,因为这两个函数都在 kernel32 中,而 kernel32 始终存在。 您需要有一些结构来返回成功/失败。
大致草图:细节留作练习! 风险:在 cmd.exe 地址空间中分配内存,更改其状态。 必须小心 DllMain 中调用的函数。
Untested, a possible approach:
Create a DLL with a DllMain that uses GetThreadStartInformation() to find the address of the buffer, and then uses GetCurrentDirectory to populate it. This should be OK, because both of those functions are in kernel32, which is always present. You will need to have some structure there to return success/failure.
Broad sketch: Details left as an excercise! Risks: Allocates memory in the cmd.exe address space, changes its state. Care must be taken with the functions called in DllMain.
您的意思是批处理文件中的 %CD% 变量吗?
像这样:
尝试在命令提示符中echo %CD%。 :)
因为这正是您所需要的,这里有一个 PowerShell 函数可以做到这一点:
希望这会有所帮助。
我从这里找到了这一切。
Do you mean the %CD% variable in a batch file ?
Like this:
Try echo %CD% in a command prompt. :)
Well as this is what you need, there here is a PowerShell function to do it:
Hope this helps.
I found it all from here.
也许这个Sysinternals 论坛上的论坛条目包含解决方案的提示。 在 GetProcessStrings 函数中查找此内容:
此 CodeProject 文章“读取远程进程的环境字符串”也可能有用。
Maybe this forum entry on the Sysinternals Forum contains a hint to the solution. Look for this in the GetProcessStrings function:
This CodeProject article "Read Environment Strings of Remote process" could be useful too.
您可以使用System.Diagnostics Namespace。这里是 C# 控制台应用程序的示例。从文件名中您可以轻松提取路径信息(System.IO.Path... )。
您需要确保拥有执行此操作的权限(以管理员身份运行)。
希望这会有所帮助。这是工作代码(经过测试):
这是输出。在我的机器上(我打开了四个命令行):
alt text http://img236 .imageshack.us/img236/3547/processworkingdirvn4.png
You could use System.Diagnostics Namespace. Here an example as C# console application. From the filename you can easily extract the Path information (System.IO.Path...).
You would make sure to have the permissions (run as admin) to do this.
Hope this helps. Here is the working code (tested):
Here the the output on my machine (I've opened four command lines):
alt text http://img236.imageshack.us/img236/3547/processworkingdirvn4.png
尝试这个简单的环境属性:
Environment.CurrentDirectory()
Try this simple environment property:
Environment.CurrentDirectory()
请参阅我对类似问题的回答(我自己)。 我编写了一个命令行实用程序和 C# 包装器来读取进程环境变量。 对于我的问题(获取 java 的当前目录),我只需读取 catalina_base 目录。
我不确定这是否直接适用于 cmd.exe。 代码项目文章中提供的实用程序无法与 cmd.exe 一起使用。
See my answer to a similar question (by myself). I wrote a command line utility and C# wrapper to read the process environment variables. For my question (getting current dir for java), I simply read the catalina_base directory.
I'm not sure if this applies directly to cmd.exe. The utility provided in the Code Project article failed to work with cmd.exe.