对文件执行操作系统命令C# [英] Execute OS Command on a file C#

查看:193
本文介绍了对文件执行操作系统命令C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过C#执行操作系统命令。以下代码取自此网页

I am trying to execute a OS command through C#. I have the following code taken from this webpage:

//Execute command on file
ProcessStartInfo procStart = 
    new ProcessStartInfo(@"C:\Users\Me\Desktop\Test\System_Instructions.txt", 
                         "mkdir testDir");

//Redirects output
procStart.RedirectStandardOutput = true;
procStart.UseShellExecute = false;

//No black window
procStart.CreateNoWindow = true;

//Creates a process
System.Diagnostics.Process proc = new System.Diagnostics.Process();

//Set start info
proc.StartInfo = procStart;

//Start
proc.Start();

但是当我尝试运行代码时,我得到以下错误:

but when I attempt to run the code I get the following error:

{指定的可执行文件不是此操作系统平台的有效应用程序。}

我做错了什么?我尝试过这个例子也是如此,但得到了

What am I doing wrong? I have tried this example as well but got the same issue.

推荐答案

ProcessStartInfo 构造函数需要一个可执行文件名和参数传递to it - .txt 文件本身不可执行。

The overload of the ProcessStartInfo constructor you are using expects an executable file name and parameters to pass to it - a .txt file is not executable by itself.

这听起来更像是要执行批处理文件与文件中的命令。对于那个检查这个SO线程:如何使用ProcessStartInfo要运行批处理文件?

It sounds more like you want to execute a batch file with commands within the file. For that check this SO thread: How do I use ProcessStartInfo to run a batch file?

这篇关于对文件执行操作系统命令C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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