简体   繁体   中英

Running batch file containing NuGet commands from C#

I have a batch file containing the following commands:

cd C:\myfolder
NuGet Update -self
NuGet pack mypackage.nuspec

myfolder contains mypackage.nuspec and NuGet.exe. I try to run this command with C# using the following function:

        private static int ExecuteCommand(string path)
        {
            ProcessStartInfo ProcessInfo;
            Process Process;

            ProcessInfo = new ProcessStartInfo(path);
            ProcessInfo.CreateNoWindow = true;
            ProcessInfo.UseShellExecute = false;
            ProcessInfo.WorkingDirectory = new System.IO.FileInfo(path).DirectoryName;
            ProcessInfo.EnvironmentVariables["EnableNuGetPackageRestore"] = "true";


            // *** Redirect the output ***
            ProcessInfo.RedirectStandardError = true;
            ProcessInfo.RedirectStandardOutput = true;


            Process = Process.Start(ProcessInfo);
            Process.WaitForExit();

            // *** Read the streams ***
            string output = Process.StandardOutput.ReadToEnd();
            string error = Process.StandardError.ReadToEnd();

            int ExitCode = Process.ExitCode;
            Process.Close();
            return ExitCode;

        }

However, my commands are not executed. What is causing this behavior and what is the solution? Those strings will probably be used in the future, I'll update my question then (just to prevent chriticism :)).

This is the final version of the function:

    private static ShellCommandReturn ExecuteCommand(string path)
    {
        ProcessStartInfo processInfo;
        Process process;

        processInfo = new ProcessStartInfo(path);
        processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        processInfo.WorkingDirectory = new System.IO.FileInfo(path).DirectoryName;
        processInfo.EnvironmentVariables["EnableNuGetPackageRestore"] = "true";


        // *** Redirect the output ***
        processInfo.RedirectStandardError = true;
        processInfo.RedirectStandardOutput = true;


        process = Process.Start(processInfo);
        process.WaitForExit();

        // *** Read the streams ***
        string output = process.StandardOutput.ReadToEnd();
        string error = process.StandardError.ReadToEnd();

        int exitCode = process.ExitCode;
        process.Close();
        return new ShellCommandReturn { Error = error, ExitCode = exitCode, Output = output };
    }

ShellCommandReturn is a simple custom class with a few data members where error, output and exit code of a shell command are stored.

Thanks.

EDIT: After a certain amount of collaboration :)

The problem is that this is executing in the context of a web application, which doesn't have the same environment variables set.

Apparently setting:

startInfo.EnvironmentVariables["EnableNuGetPackageRestore"] = "true"

(using the naming of my final code below) fixes the problem.


Old answer (still worth reading)

Look at this code:

ProcessInfo = new ProcessStartInfo(path);
ProcessInfo.CreateNoWindow = false;
ProcessInfo.UseShellExecute = true;
ProcessInfo.WorkingDirectory = new System.IO.FileInfo(path).DirectoryName;

Process = Process.Start(path);

You're creating a ProcessStartInfo , but then completely ignoring it. You should be passing it into Process.Start . You should also rename your variables. Conventionally local variables start with lower case in C#. Additionally, it's a good idea to initialize variables at the point of first use, where possible. Oh, and import namespaces so you don't fully qualified names such as System.IO.FileInfo in your code. Finally, object initializers are useful for classes like ProcessStartInfo :

var startInfo = new ProcessStartInfo(path) {
    CreateNoWindow = false,
    UseShellExecute = true,
    WorkingDirectory = new FileInfo(path).DirectoryName;
};
var process = Process.Start(startInfo);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM