Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute OS Command on a file C#

Tags:

c#

cmd

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.

like image 733
Soatl Avatar asked Dec 10 '22 06:12

Soatl


2 Answers

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.

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?

like image 110
BrokenGlass Avatar answered Jan 04 '23 04:01

BrokenGlass


Try setting the USESHELLEXECUTE member to TRUE instead of FALSE.

It worked for me - but I think this has reprocussions for certain users after publishing.

like image 36
else Avatar answered Jan 04 '23 03:01

else