I've got four things for you that you can try out:
(1) Try providing the full path for cmd.exe
(e.g. on my machine: C:\WINDOWS\SYSTEM32\CMD.EXE
).
(2) Try adding call
to the command to be executed:
Process.Start(@"C:\WINDOWS\SYSTEM32\CMD.EXE", @"/c call D:\a.cmd");
(3) Besides that, I can only guess where the %1
in the Win32Exception
is coming from. Maybe your file associations are set-up incorrectly.
If you type the following on the command-line:
> assoc .cmd
You will likely get a mention of cmdfile
. If you then look up this token with:
> ftype cmdfile
You might get an answer along the lines of:
cmdfile="%1" %*
Those settings are stored in the registry, and this is how the command-line interpreter knows how to execute files with custom extensions. (You can find out how a PDF document is started by executing the above two statements for the .pdf
extension.)
(4) If you start to suspect that your machine might be mis-configured, start regedit
(the registry editor) and locate the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
.
On my Windows XP machine (and your Process.Start
example works on my machine, with different filenames though), I've got the following values stored in there:
// Name Type Value
// -----------------------------------------------
// (standard) REG_SZ (not set)
// AutoRun REG_SZ
// CompletionChar REG_DWORD 0x00000040 (64)
// DefaultColor REG_DWORD 0x00000000 (0)
// EnableExtensions REG_DWORD 0x00000001 (1)
// PathCompletionChar REG_DWORD 0x00000040 (64)
Of those, the AutoRun
value might be of some interest. I think it corresponds to the /d
command-line switch of cmd.exe
, which controls whether cmd.exe
attempts to start files with custom extensions. Usually, this is enabled. Maybe, on your machine, it isn't?