在C#中运行Bat文件 [英] Run a Bat File in C#

查看:593
本文介绍了在C#中运行Bat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!
我正在尝试运行一个bat文件并为一个工业应用程序编程一个IC.但是当我运行以下代码时,出现了这样的异常,

Hello!
I am trying to run a bat file and programme an IC for an indstrl applctn.But when i run the following code i get an exception like that,

"WIN32Exception was unhandled"<br />
"Access is denied"



如果您能为我解决这个问题提供任何帮助,我将非常感谢.
谢谢.

代码:



I will be very thankful to you if you can give me any kind of help to solve this problem.
Thank you.

Code:

string result = "";
int response = 0;

System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"G:\Projects\FEIN LIION\Documents Fien Lion\Documents\FEIN Akku_LiION_S3515\Software_423_303044_V0_10");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listfiles;

listfiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myoutput = listfiles.StandardOutput;
listfiles.WaitForExit(2000);

if (listfiles.HasExited)
{
    result = myoutput.ReadToEnd();
    response = listfiles.ExitCode;
}
MessageBox.Show(response.ToString());
}

推荐答案

一些可以尝试的基本步骤

1)如果要写入资源,请确保另一个进程没有访问同一资源(如果意外在资源文件中打开该文件,请关闭该文件).
2)确保您具有访问该文件的权限.
3)如果您运行的是Vista,则可能需要更改UAC设置以允许访问资源(尽管并非总是建议这样做).
4)您可能需要成为管理员才能打开/编辑此资源.
Some basic steps that you could try out

1) If you are writing to a resource make sure another process is not accessing the same resource (close the file if it is accidentally open in a resource file).
2) Make sure you have permissions to access the file.
3) If you are running Vista, you might need to change UAC settings to allow access to the resource (though this is not always a recommended practise).
4) You might need to be an administrator to open / edit this resource.


一个问题是您的路径名包含空格.您需要使用引号获取此类路径名.另一个问题:我看不到批处理文件名.名称"Software_423_303044_V0_10"看起来不正确.它可以是不同的文件名或目录名.或批次名称,但".bat"在哪里?

它应该看起来像这样:

One problem is that your path name contains blank spaces. You need to get such path names in quotation marks. Another problem: I don''t see batch file name. The name "Software_423_303044_V0_10" looks wrong. It can be some different file or directory name; or a batch name, but where is ".bat"?

It should look like this:

string pathName =
  @"""G:\Projects\FEIN LIION\Documents Fien Lion\Documents\FEIN Akku_LiION_S3515\MyBatchFile.bat""";
//pay attention for """ above



绝对路径不好!这样的东西即使从一台计算机到另一台计算机也应该是便携式的.在调用Process.Start时,该路径应该是绝对的,但绝不能进行硬编码.应该始终根据配置参数或入口组件的位置或两者来计算is.几乎没有这样的情况,硬编码的文件名可以工作.

通常,立即数常量是邪恶的.使用显式常量,显式只读属性,资源值或…变量,从配置文件读取的数据.例如,像这样使用"是不好的,应该使用system.Empty.

您的例外可能在其他地方.如果您没有为异常堆栈提供代码行号以及代码的相应部分,那么报告异常问题的用途是什么?使用调试器指出有问题的代码的确切行,并在您的代码示例中进行指示.

-SA



Absolute path is bad! How such thing is supposed to be portable even from one computer to another. The path should be absolute at the moment of the call to Process.Start but never hard-coded. Is should always be calculated based on configuration parameters, or the location of entry assembly or both. There are practically no such cases when a hard-coded file name could work.

Immediate constants is evil, in general. Use explicit constants, explicit read-only properties of, resource values or… variables, data read from configuration files. For example, using "" like you do is bad, you should use system.Empty.

Your exception can be somewhere else. What''s the use of reporting exception problem if you do not provide exception stack with code line numbers, along with corresponding part of code? Use debugger to point out exact line of offending code and indicate it in your code sample.

—SA


http://blogs.msdn.com/b/csharpfaq/archive/2004/06/01/146375.aspx[^]

May be here you will get the exact things.


这篇关于在C#中运行Bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆