开发者

Execute OS Command on a file 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:

{"The specified executable is not a val开发者_如何学Cid application for this OS platform."}

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


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?


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.


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.


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.


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).


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?)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜