1

次のコマンドを含むバッチファイルがあります。

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;

        }

しかし、私のコマンドは実行されません。この動作の原因と解決策は何ですか?これらの文字列はおそらく将来使用されるでしょう、それから私の質問を更新します(ただ批判を防ぐために:))。

これは、関数の最終バージョンです。

    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は、シェルコマンドのエラー、出力、および終了コードが格納されているいくつかのデータメンバーを持つ単純なカスタムクラスです。

ありがとう。

4

1 に答える 1

1

編集:ある程度のコラボレーションの後:)

問題は、これが同じ環境変数が設定されていない 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);
于 2012-11-12T15:38:23.030 に答える