启动cmd.exe问题
在 Windows 中,当打开命令提示符 (cmd.exe) 时,注册表项:
HKLM\Software\Microsoft\命令处理器
HKCU\Software\Microsoft\命令处理器
检查 是否有名为“AutoRun”的值。如果找到,则执行该值中指定的批处理文件,提供类似自动执行的功能。如果两个键都包含 AutoRun 值,则两者都将运行。惊人的!
我正在使用 Process.Start 来运行 cmd.exe,但没有发生自动运行行为。我当前的代码是:
private openShell( string folder )
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = Environment.GetEnvironmentVariable( "COMSPEC" ) ?? "cmd.exe",
Arguments = "/k cd \"" + folder + "\"",
UseShellExecute = true
};
try
{
using ( var exeProcess = System.Diagnostics.Process.Start( startInfo ) )
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
}
我还尝试将其简化为最简单的形式:
System.Diagnostics.Process.Start( "cmd.exe" );
我尝试的一切都完美运行(启动命令窗口),但自动运行行为从未发生。
In Windows, when a command prompt is opened (cmd.exe), the registry keys:
HKLM\Software\Microsoft\Command Processor
HKCU\Software\Microsoft\Command Processor
are checked for a value called "AutoRun". If found, the batch file named in the value is executed, providing autoexec-like functionality. If both keys contain AutoRun values, both will be run. Awesome!
I'm using Process.Start
to run cmd.exe and the AutoRun behavior is not occuring. My current code is:
private openShell( string folder )
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = Environment.GetEnvironmentVariable( "COMSPEC" ) ?? "cmd.exe",
Arguments = "/k cd \"" + folder + "\"",
UseShellExecute = true
};
try
{
using ( var exeProcess = System.Diagnostics.Process.Start( startInfo ) )
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
}
I've also tried reducing it down to the simplest form:
System.Diagnostics.Process.Start( "cmd.exe" );
Everything I try works perfectly (in that a command window is launched), but the AutoRun behavior never occurs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了。问题在于 Windows 如何在 64 位版本的操作系统中存储(和检索)注册表中的数据。我的代码很好。 MSDN 上的文章介绍了一些详细信息。摘自那篇文章:
对我来说,解决方案是将我的 AutoRun 键添加到:
真正让我生气的是我以前遇到过这个问题。我很少玩弄注册表,我完全忘记了它。
I found it. The problem was in how Windows stores (and retrieves) data from the registry in 64-bit versions of the operating system. My code was fine. The article on MSDN goes into some detail. From that article:
The solution for me was to add my AutoRun key under:
What really gets my goat is that I've run into this before. I so rarely fool with the registry, I'd just completely forgotten about it.