Executing Batch File in C#

前端 未结 12 1775
有刺的猬
有刺的猬 2020-11-22 05:22

I\'m trying to execute a batch file in C#, but I\'m not getting any luck doing it.

I\'ve found multiple examples on the Internet doing it, but it is not working for

相关标签:
12条回答
  • 2020-11-22 05:47

    Have you tried starting it as an administrator? Start Visual Studio as an administrator if you use it, because working with .bat files requires those privileges.

    0 讨论(0)
  • 2020-11-22 05:49
    using System.Diagnostics;
    
    private void ExecuteBatFile()
    {
        Process proc = null;
        try
        {
            string targetDir = string.Format(@"D:\mydir");   //this is where mybatch.bat lies
            proc = new Process();
            proc.StartInfo.WorkingDirectory = targetDir;
            proc.StartInfo.FileName = "lorenzo.bat";
            proc.StartInfo.Arguments = string.Format("10");  //this is argument
            proc.StartInfo.CreateNoWindow = false;
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;  //this is for hiding the cmd window...so execution will happen in back ground.
            proc.Start();
            proc.WaitForExit();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:52

    Using CliWrap:

    var result = await Cli.Wrap("foobar.bat").ExecuteBufferedAsync();
    
    var exitCode = result.ExitCode;
    var stdOut = result.StandardOutput;
    
    0 讨论(0)
  • 2020-11-22 05:53

    System.Diagnostics.Process.Start(BatchFileName, Parameters);

    I know this will work for batch file and parameters, but no ideas how to get the results in C#. Usually, the outputs are defined in the batch file.

    0 讨论(0)
  • 2020-11-22 05:54

    This should work. You could try to dump out the contents of the output and error streams in order to find out what's happening:

    static void ExecuteCommand(string command)
    {
        int exitCode;
        ProcessStartInfo processInfo;
        Process process;
    
        processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
        processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        // *** Redirect the output ***
        processInfo.RedirectStandardError = true;
        processInfo.RedirectStandardOutput = true;
    
        process = Process.Start(processInfo);
        process.WaitForExit();
    
        // *** Read the streams ***
        // Warning: This approach can lead to deadlocks, see Edit #2
        string output = process.StandardOutput.ReadToEnd();
        string error = process.StandardError.ReadToEnd();
    
        exitCode = process.ExitCode;
    
        Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
        Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
        Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
        process.Close();
    }
    
    static void Main()
    {
        ExecuteCommand("echo testing");
    }   
    

    * EDIT *

    Given the extra information in your comment below, I was able to recreate the problem. There seems to be some security setting that results in this behaviour (haven't investigated that in detail).

    This does work if the batch file is not located in C:\Windows\System32. Try moving it to some other location, e.g. the location of your executable. Note that keeping custom batch files or executables in the Windows directory is bad practice anyway.

    * EDIT 2 * It turns out that if the streams are read synchronously, a deadlock can occur, either by reading synchronously before WaitForExit or by reading both stderr and stdout synchronously one after the other.

    This should not happen if using the asynchronous read methods instead, as in the following example:

    static void ExecuteCommand(string command)
    {
        var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
        processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        processInfo.RedirectStandardError = true;
        processInfo.RedirectStandardOutput = true;
    
        var process = Process.Start(processInfo);
    
        process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
            Console.WriteLine("output>>" + e.Data);
        process.BeginOutputReadLine();
    
        process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
            Console.WriteLine("error>>" + e.Data);
        process.BeginErrorReadLine();
    
        process.WaitForExit();
    
        Console.WriteLine("ExitCode: {0}", process.ExitCode);
        process.Close();
    }
    
    0 讨论(0)
  • 2020-11-22 05:55

    Here is sample c# code that are sending 2 parameters to a bat/cmd file for answer this question.

    Comment: how can I pass parameters and read a result of command execution?

    /by @Janatbek Sharsheyev

    Option 1 : Without hiding the console window, passing arguments and without getting the outputs

    • This is an edit from this answer /by @Brian Rasmussen
    using System;
    using System.Diagnostics;
    
    
    namespace ConsoleApplication
    {
        class Program
        { 
            static void Main(string[] args)
            {
             System.Diagnostics.Process.Start(@"c:\batchfilename.bat", "\"1st\" \"2nd\"");
            }
        }
    }
    

    Option 2 : Hiding the console window, passing arguments and taking outputs


    using System;
    using System.Diagnostics;
    
    namespace ConsoleApplication
    {
        class Program
        { 
            static void Main(string[] args)
            {
             var process = new Process();
             var startinfo = new ProcessStartInfo(@"c:\batchfilename.bat", "\"1st_arg\" \"2nd_arg\" \"3rd_arg\"");
             startinfo.RedirectStandardOutput = true;
             startinfo.UseShellExecute = false;
             process.StartInfo = startinfo;
             process.OutputDataReceived += (sender, argsx) => Console.WriteLine(argsx.Data); // do whatever processing you need to do in this handler
             process.Start();
             process.BeginOutputReadLine();
             process.WaitForExit();
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题