4

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.

Jason Down
  • 21,731
  • 12
  • 83
  • 117
Soatl
  • 10,224
  • 28
  • 95
  • 153
  • You try to execute a Textfile, which (as the error indicates) is nto executable. If you want to create a folder, I would recommend to use the System.IO classes, like DirectoryInfo, Fileinfo etc. – Bernhard Kircher Aug 09 '11 at 21:12
  • It is totally unclear from your description what it is that you are trying to accomplish. – MarkPflug Aug 09 '11 at 21:38

7 Answers7

7

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?

Community
  • 1
  • 1
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • That makes sense. How would I go about doing this with non .exe files then? – Soatl Aug 09 '11 at 21:12
  • 2
    @Peppered what's the "this" that you're wanting to do? Do you want to open the file in notepad? Do you want to write "mkdir testDir" into that file? Do you just want to execute "mkdir testDir" without anything to do with the text file? Do you want to execute the statements inside the text file? – Davy8 Aug 09 '11 at 21:16
  • I want to take a bunch of files, save them (already have that part done) and then execute some command (i dont know what it will be) on them. I also don't know what type of files I will have – Soatl Aug 10 '11 at 13:24
4

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.

else
  • 872
  • 6
  • 15
2

You are trying to execute a TXT file. That's why you get

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

Because, well, the specified executable (TXT) is not a valid application for this OS platform.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
1

You would target an executable or other file that has a specified opening application. You're targeting a text file; what you should do is target Notepad, and then supply the path to your text file as an argument:

ProcessStartInfo info = new ProcessStartInfo
{
    FileName = "C:\\Windows\System32\\notepad.exe",
    Arguments = "C:\\Users\\Me\\Desktop\\Test\\System_Instructions.txt"
}

new Process.Start(info);

Alternatively, if you mean for your text file to be executed, it needs to be made a .bat file.

Tejs
  • 40,736
  • 10
  • 68
  • 86
0

You are trying to execute this:

C:\Users\Me\Desktop\Test\System_Instructions.txt mkdir testDir

The shell has no clue how to "execute" a text file so the command fails.

If you want to execute this text file as a batch file, change file extension to .bat so the system understands it's a batch file, and then set UseShellExecute so it does the default action for it (= runs it, in case of a batch file).

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
0

If you want to open up the file in Notepad, use:

ProcessStartInfo procStart = 
    new ProcessStartInfo("notepad.exe", @"C:\Users\Me\Desktop\Test\System_Instructions.txt");

If you want to write into the file :

        //In case the directory doesn't exist
        Directory.CreateDirectory(@"C:\Users\Me\Desktop\Test\);
        using (var file = File.CreateText(@"C:\Users\Me\Desktop\Test\System_Instructions.txt"))
        {
            file.WriteLine("mkdir testDir");
        }

If you have commands in the text file that you want to execute, just rename it to .bat and it should work (and presumably the contents do something with "mkdir testDir" as a parameter?)

Davy8
  • 30,868
  • 25
  • 115
  • 173
0

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.

MarkPflug
  • 28,292
  • 8
  • 46
  • 54
  • I want to take a bunch of files, save them (already have that part done) and then execute some command (i dont know what it will be) on them. I also don't know what type of files I will have – Soatl Aug 10 '11 at 13:24