问题描述
(请参阅解决完)
我不认为这将是困难的。我有一个条命令的文件,D:\\ a.cmd包含:
I didn't think this was going to be hard. I have a commmand file, d:\a.cmd which contains:
copy /b d:\7zS.sfx + d:\config.txt + d:\files.7z d:\setup.exe
但C#这些线路将不会执行它:
But these lines of C# won't execute it:
Process.Start("d:\\a.cmd");
Process.Start("cmd", "/c d:\\a.cmd");
抛出Win32Exception:%1不是有效的Win32应用程序
Throws Win32Exception: "%1 is not a valid Win32 application."
打开的Process.Start .pdf文件...为什么不执行命令文件?
Process.Start opens .pdf files...why not execute command files?
这工作,如果我在一个cmd窗口键入:
This works if I type it in a cmd window:
cmd /c d:\a.cmd
Windows XP中,微软的Visual Studio 2008中。
Windows XP, MS Visual Studio 2008.
在此先感谢,
吉姆
Thanks in advance,Jim
解决方案
我只是有点尴尬:(有我的应用程序的目录名为cmd.exe的文件,大小为零。我不知道如何到达那里,但它现在是烤面包和上述两个C#语句现在的工作,我出发寻找哈利·波特的书,所以我可以从多比得到一些自我惩罚的想法......
SOLUTIONI'm only SLIGHTLY embarrassed :( There was a file named cmd.exe, size zero in my app's dir. I have no idea how it got there but it is now toast and both of the above C# statements now work. I'm off to find a Harry Potter book so I can get some self-punishment ideas from Dobby...
推荐答案
我有四件事情为你,你可以尝试一下:
I've got four things for you that you can try out:
(1)尝试提供完整的路径的cmd.exe
(例如:我的机器上: C:\\ WINDOWS \\ SYSTEM32 \\ CMD.EXE
)。
(1) Try providing the full path for cmd.exe
(e.g. on my machine: C:\WINDOWS\SYSTEM32\CMD.EXE
).
(2)尝试添加呼叫
将要执行的命令:
(2) Try adding call
to the command to be executed:
Process.Start(@"C:\WINDOWS\SYSTEM32\CMD.EXE", @"/c call D:\a.cmd");
(3)除此之外,我只能猜测,其中%1
在 Win32Exception
的来源。也许你的文件关联设置不正确。
(3) Besides that, I can only guess where the %1
in the Win32Exception
is coming from. Maybe your file associations are set-up incorrectly.
如果您键入的命令行:
> assoc .cmd
您可能会得到的CMDFILE
一提。如果你再看看这个令牌:
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" %*
这些设置存储在注册表中,这是命令行间preTER怎么知道如何与自定义扩展执行文件。 (你可以找到如何PDF文档被执行的 .PDF
扩展上面的两个语句开始。)
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)如果你开始怀疑你的机器可能是错误配置,启动注册表编辑器
(注册表编辑器)并找到键 HKEY_LOCAL_MACHINE \\ SOFTWARE \\微软\\命令处理器
。
(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
.
在我的Windows XP机器(和你的的Process.Start
例如工作在我的机器上,用不同但文件名),我有存储在那里以下值:
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)
其中,在自动运行
值可能的一些利益。我认为它对应的 /天
命令行的cmd.exe
的开关,控制是否 CMD.EXE
尝试与自定义扩展启动文件。通常,这被启用。也许,你的机器上,难道不是吗?
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?
这篇关于如何执行从C#批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!