Ash*_*Ash 16 .net directory winapi cmd
如何检索cmd.exe的当前工作目录?
这似乎是可能的.例如,使用ProcessExplorer,选择CMD.exe,右键单击,属性,图像选项卡,"当前目录"使用CD或CHDIR命令关联目录集.
我查看了.NET Process和ProcessStartInfo类(ProcessStartInfo.WorkingDirectory总是返回""),似乎无法找到确定这一点的方法.PInvoke的任何一个都不突出.
作为一个例子,我希望以编程方式能够说出类似:Process.GetCurrentWorkingDirectory(processID),其中processID是另一个正在运行的进程的Windows进程ID.
有没有解决方案,WinAPI或.NET?
[更新]
提出这个问题的原因:
我已经使用了"命令提示符浏览器栏"一段时间,它很棒,除非我"CD"到一个新目录,当前的资源管理器窗口也没有改变.(即,从资源管理器到命令提示符只有1路同步).我想要做到这一点.
未经测试,可能的方法:
使用DllMain创建一个DLL,该DLL使用GetThreadStartInformation()来查找缓冲区的地址,然后使用GetCurrentDirectory填充它.这应该没问题,因为这两个函数都在kernel32中,它始终存在.您需要在那里有一些结构才能返回成功/失败.
广泛的草图:作为练习留下的细节!风险:在cmd.exe地址空间中分配内存,更改其状态.必须注意DllMain中调用的函数.
您是指批处理文件中的%CD%变量吗?
像这样:
set OLDDIR=%CD%
.. do stuff ..
chdir /d %OLDDIR% &rem restore current directory
Run Code Online (Sandbox Code Playgroud)
在命令提示符中尝试echo%CD%.:)
这就是你需要的,这里有一个PowerShell函数:
$(get-location)
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
我从这里发现了这一切.
也许在Sysinternals论坛上的这个论坛条目包含解决方案的提示.在GetProcessStrings函数中查找:
// Get Command Line Block
// At offset 0x00020498 is the process current directory followed by
// the system PATH. After that is the process full command line, followed
// by the exe name and the windows station it's running on.
Run Code Online (Sandbox Code Playgroud)
此CodeProject文章" 读取远程进程的环境字符串 "也很有用.