开发者

Starting cmd.exe problem

开发者 https://www.devze.com 2023-01-27 01:26 出处:网络
In Windows, when a command prompt is opened (cmd.exe), the registry keys: HKLM\\Software\\Microsoft\\Command Processor

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.GetEnvi开发者_C百科ronmentVariable( "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.


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:

On 64-bit Windows, portions of the registry entries are stored separately for 32-bit application and 64-bit applications and mapped into separate logical registry views using the registry redirector and registry reflection, because the 64-bit version of an application may use different registry keys and values than the 32-bit version. There are also shared registry keys that are not redirected or reflected.

The solution for me was to add my AutoRun key under:

HKLM\Software\Wow6432Node\Microsoft\Command Processor

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消