繁体   English   中英

从C#运行包含NuGet命令的批处理文件

[英]Running batch file containing NuGet commands from C#

我有一个包含以下命令的批处理文件:

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

myfolder包含mypackage.nuspec和NuGet.exe。 我尝试使用以下功能通过C#运行此命令:

        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;

        }

但是,我的命令没有执行。 是什么引起了这种现象,如何解决? 这些字符串可能会在将来使用,然后我将更新我的问题(只是为了防止chriticism :))。

这是该功能的最终版本:

    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是一个简单的自定义类,具有几个数据成员,用于存储shell命令的错误,输出和退出代码。

谢谢。

编辑:经过一定程度的协作:)

问题在于这是在Web应用程序的上下文中执行的,该Web应用程序没有设置相同的环境变量。

显然设置:

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

(使用下面的最终代码命名)解决了该问题。


旧答案 (仍然值得一读)

看这段代码:

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

Process = Process.Start(path);

您正在创建ProcessStartInfo ,但随后完全忽略它。 您应该将其传递给Process.Start 您还应该重命名变量。 传统上,局部变量以C#中的小写字母开头。 另外,最好在首次使用时初始化变量。 哦,导入名称空间,这样您就不会在代码中使用完全限定的名称,例如System.IO.FileInfo 最后,对象初始化程序对于诸如ProcessStartInfo类的类很有用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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