Execute OS Command on a file C#

2019-02-20 02:18发布

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:

{"The specified executable is not a valid application for this OS platform."}

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

标签: c# cmd
7条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-20 02:47

What are you trying to accomplish? Create a directory? Use the "System.IO.Directory.CreateDirectory" method. Open a .txt file with associated program? Use ProcessStartInfo(@".\filename.txt") with UseShellExecute set to true. This will cause the associated program for that file type to be executed, which might not be notepad.txt.

查看更多
登录 后发表回答