问题描述
我需要做的是有一个C#2005 GUI应用程序在调用用户的请求一个.BAT和几个VBScript的文件。这只是一个权宜之计,直到假期结束,我可以一直写C#。我能得到的VBScript文件没有问题要执行,但我无法执行.bat文件。当我在C#应用程序,单击执行.bat文件DOS窗口打开和关闭速度非常快,测试.bat文件不会执行 - Windows无法识别蝙蝠作为一个内部或外部命令是错误在DOS框中返回。如果我只是域金字塔之戒单击该.bat文件或手动从命令来运行它促使它执行。我还需要.bat文件默默执行,除非需要进行用户交互 - 这个脚本副本11K +联网的计算机上的文件,文件夹和偶尔的Windows忘记如果目标是一个文件或目录,并要求用户告诉它它是什么(这是一个整体的其他问题不适合在这里讨论的......不用说,我被它生气)。
所以在我的C#源远我有这样的:
过程scriptProc =新工艺();
如果(File.Exists(C:\\\\ \\\\脚本batchfile1.bat))
{ scriptProc.StartInfo.FileName = @的cscript;
scriptProc.StartInfo.Arguments =(cmd.exe的,/ C C:\\\\ \\\\脚本batchfile1.bat); //古怪的伪code //
scriptProc.Start();
scriptProc.WaitForExit(1500000);
scriptProc.Close(); } 如果(File.Exists(!C:\\\\ \\\\脚本batchfile1.bat))
{
}
我知道这code不工作 - 但它本质上是我想要做的事。我所看到的是像这样的.bat文件。我想我必须告诉系统使用CMD运行.BAT。我很茫然至于如何做到这一点。我已签出此这是为C#2003没有太大的帮助我,我很绿色的C#。
编辑:使用凯文的职位,我再次尝试它。从该职位相同的解决方案脚本,但修改了我,因为我不需要重定向:
的System.Diagnostics.Process PROC =新的System.Diagnostics.Process();
proc.StartInfo.FileName =C:\\\\ \\\\脚本batchfile1.bat
proc.StartInfo.RedirectStandardError = FALSE;
proc.StartInfo.RedirectStandardOutput = FALSE;
proc.StartInfo.UseShellExecute = FALSE;
proc.Start();
proc.WaitForExit();
下面是你在找什么:
http://stackoverflow.com/questions/361097/c-service-cannot-execute-batch-file
这是关于,为什么服务不能执行的文件的问题,但它显示了所有必要的code这样做。
What I need to do is have a C# 2005 GUI app call a .bat and several VBScript files at user's request. This is just a stop-gap solution until the end of the holidays and I can write it all in C#. I can get the VBScript files to execute with no problem but I am unable to execute the .bat file. When I "click" in the C# app to execute the .bat file a DOS window opens up and closes very fast and the test .bat file does not execute - "Windows does not recognize bat as an internal or external command" is the error returned in the DOS box. If I simply doubl-click the .bat file or manually run it from the command prompt it does execute. I also need the .bat file to execute silently unless a user interaction is required - this script copies 11k+ files to folders on a networked machine and occasionally Windows "forgets" if the destination is a file or directory and asks for the user to tell it what it is (this is a whole other issue not for discussion here...needless to say I am annoyed by it).
So far in my C# source I have this :
Process scriptProc = new Process();
if (File.Exists("c:\\scripts\\batchfile1.bat"))
{
scriptProc.StartInfo.FileName = @"cscript";
scriptProc.StartInfo.Arguments = ("cmd.exe", "/C C:\\scripts\\batchfile1.bat"); // Wacky psuedo code //
scriptProc.Start();
scriptProc.WaitForExit(1500000);
scriptProc.Close();
}
if (!File.Exists("c:\\scripts\\batchfile1.bat"))
{
}
I am aware that this code does not work - but it is essentially what I want it to do. What I am looking at is something like this for .bat files. I assume I have to tell the system to use cmd to run the .bat. I am at a loss as to how to do this. I have checked out this site which is for C# 2003. Not much help for me as I am very green with C#.
EDIT: Using Kevin's post I attempted it again. Same solution script from that post but modified for me since I do not need to redirect:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "C:\\scripts\\batchfile1.bat";
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
Here is what you are looking for:
http://stackoverflow.com/questions/361097/c-service-cannot-execute-batch-file
It's about a question as to why a service can't execute a file, but it shows all the code necessary to do so.
这篇关于如何执行从C#Windows窗体应用程序.bat文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!