如何从 C# 执行批处理文件?
(解决方案见结尾)
我认为这不会很难。我有一个命令文件 d:\a.cmd,其中包含:
copy /b d:\7zS.sfx + d:\config.txt + d:\files.7z d:\setup.exe
但这些 C# 行不会执行它:
Process.Start("d:\\a.cmd");
Process.Start("cmd", "/c d:\\a.cmd");
抛出 Win32Exception:“%1 不是有效的 Win32 应用程序。”
Process.Start 打开 .pdf 文件...为什么不执行命令文件?
如果我在 cmd 窗口中键入它,则此方法有效:
cmd /c d:\a.cmd
Windows XP、MS Visual Studio 2008。
提前致谢, 吉姆
解决方案 我只是有点尴尬:( 我的应用程序目录中有一个名为 cmd.exe 的文件,大小为零。我不知道它是如何到达那里的,但它现在是 toast 并且上述两个 C# 语句现在都可以工作。我去找一本哈利·波特的书,这样我就可以从多比那里得到一些自我惩罚的想法……
(See end for solution)
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
But these lines of C# won't execute it:
Process.Start("d:\\a.cmd");
Process.Start("cmd", "/c d:\\a.cmd");
Throws Win32Exception: "%1 is not a valid Win32 application."
Process.Start opens .pdf files...why not execute command files?
This works if I type it in a cmd window:
cmd /c d:\a.cmd
Windows XP, MS Visual Studio 2008.
Thanks in advance,
Jim
SOLUTION
I'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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我为您提供了四件事供您尝试:
(1) 尝试提供
cmd.exe
的完整路径(例如我的机器:C:\WINDOWS\SYSTEM32\CMD.EXE
)。(2) 尝试在要执行的命令中添加
call
:(3) 除此之外,我只能猜测
% 在哪里
来自。也许您的文件关联设置不正确。Win32Exception
中的 1如果您在命令行中键入以下内容:
您可能会提到
cmdfile
。如果您随后使用以下命令查找此标记:您可能会得到如下答案:
这些设置存储在注册表中,这就是命令行解释器知道如何执行具有自定义扩展名的文件的方式。 (您可以通过执行
.pdf
扩展名的上述两条语句来了解 PDF 文档是如何启动的。)(4) 如果您开始怀疑您的机器可能如果配置错误,请启动
regedit
(注册表编辑器)并找到键HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
。在我的 Windows XP 计算机上(您的 Process.Start 示例在我的计算机上运行,但文件名不同),我在那里存储了以下值:
其中,
AutoRun< /code> 值可能会引起一些兴趣。我认为它对应于
cmd.exe
的/d
命令行开关,它控制cmd.exe
是否尝试使用自定义启动文件扩展。通常,此功能已启用。也许,在你的机器上,不是吗?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:(3) Besides that, I can only guess where the
%1
in theWin32Exception
is coming from. Maybe your file associations are set-up incorrectly.If you type the following on the command-line:
You will likely get a mention of
cmdfile
. If you then look up this token with:You might get an answer along the lines of:
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 keyHKEY_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:Of those, the
AutoRun
value might be of some interest. I think it corresponds to the/d
command-line switch ofcmd.exe
, which controls whethercmd.exe
attempts to start files with custom extensions. Usually, this is enabled. Maybe, on your machine, it isn't?或者可以做一个.bat文件,然后通过
调用这个文件System.Diagnostics.Process.Start()。它不会将输出重定向到控制台应用程序,但它肯定会执行其中的命令。
Or you can do a .bat file, then call this file through
System.Diagnostics.Process.Start()
. It won't redirect output to Console Application, but it would certainly execute the commands inside.您需要指定进程全名(cmd.exe)。
您应该尝试
这样,即使 cmd.exe 位于您的应用程序目录中,您也可以确保执行正确的文件。
You need to specify the process full name (cmd.exe).
You should try
So you can be sure to execute the right file even if a cmd.exe is in your applications directory.
您的计算机看起来有问题。尝试在另一台机器上运行它。这应该可以工作。
Process.Start(string)
使用ShellExecuteEx
启动文件,因此正如您所想,它与在资源管理器中双击文件几乎相同。一个简单的测试对我有用。
B:\foo.cmd:
Program.cs:
这按预期工作。
您的错误消息很可疑,“%1 不是有效的 Win32 应用程序。”我的注册表 HKCR\cmdfile\shell\open\command 中的值是
“%1”%*
%1
被文件名替换,并且%*
在这里可以被忽略(它表明任何进一步的命令行参数都应该被传递) ,但我们现在不关心这个)。文件本身被启动来处理这种类型的文件这一事实表明 Windows 本身知道如何启动这种类型的文件。在正常安装的 Windows 上,应类似地设置以下扩展名:
.exe
Windows 和 DOS 可执行文件.com
DOS“命令”文件.bat
Windows 和 DOS 批处理文件.cmd
Windows NT 批处理文件.pif
DOS 可执行文件的 Windows 快捷方式如果您转到
HKCR\ .xxx
(其中xxx
是上述任意一项),“(默认)”值应为xxxfile
。如果您随后转到HKCR\xxxfile\shell\open\command
,“(默认)”值应为“%1” %*
。此外,HKCR\xxxfile\shell
的“(默认)”值应该不设置或打开
。如果这些值中有任何其他值,则表明某个程序已尝试将自身插入到执行过程中。病毒有时会这样做(例如,Sircam)。
It looks like something wrong with your computer. Try running this on another machine. This should work.
Process.Start(string)
usesShellExecuteEx
to launch the file, so it's pretty much the same thing as double-clicking the file in Explorer, as you supposed.A simple test worked for me.
B:\foo.cmd:
Program.cs:
This works as expected.
Your error message is suspicious, "%1 is not a valid Win32 application." The value in my registry at HKCR\cmdfile\shell\open\command is
"%1" %*
The
%1
gets replaced by the file name, and the%*
can be ignored here (it indicates that any further command-line arguments should be passed along, but we're not concerned with that right now).The fact that the file itself is launched to handle this type of file indicates that Windows itself knows how to launch this type of file. On a normal installation of Windows, the following extensions should be set up similarly:
.exe
Windows and DOS executable files.com
DOS "command" files.bat
Windows and DOS batch files.cmd
Windows NT batch files.pif
Windows shortcuts to DOS executable filesIf you go to
HKCR\.xxx
(wherexxx
is any of the above), the "(Default)" value should bexxxfile
. If you then go toHKCR\xxxfile\shell\open\command
, the "(Default)" value should be"%1" %*
. Also the "(Default)" value ofHKCR\xxxfile\shell
should be either not set oropen
.If you have anything else in any of these values, then some program has attempted to insert itself into the execution process. Viruses sometimes do this (Sircam, for example).
您是否尝试过执行
cmd.exe
并将 .cmd 文件作为参数传递给它?Have you tried executing
cmd.exe
, and passing the .cmd file to it as an argument?嗯尝试:
hmm try:
您是否在目录中测试了批处理文件,它将运行的上下文? %1 的错误消息看起来问题可能在那里?
Have you tested your batch file in the directory, context it's going to run? The error message with %1 looks like the problem may be in there?