C# 运行包含来自C的NuGet命令的批处理文件#
我有一个包含以下命令的批处理文件:C# 运行包含来自C的NuGet命令的批处理文件#,c#,asp.net,shell,nuget,C#,Asp.net,Shell,Nuget,我有一个包含以下命令的批处理文件: cd C:\myfolder NuGet Update -self NuGet pack mypackage.nuspec myfolder包含mypackage.nuspec和NuGet.exe。我尝试使用C#使用以下函数运行此命令: private static int ExecuteCommand(string path) { ProcessStartInfo ProcessInfo;
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 };
}
但是,不会执行我的命令。是什么导致了这种行为?解决方案是什么?这些字符串将来可能会被使用,我将更新我的问题(只是为了防止使用chriticism:)
这是函数的最终版本:
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是一个简单的自定义类,有几个数据成员,其中存储了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);
您正在创建一个进程startinfo
,但随后完全忽略它。您应该将其传递到进程。启动。您还应该重命名变量。传统上,局部变量以C#中的小写字母开始。此外,在可能的情况下,最好在首次使用时初始化变量。哦,导入名称空间,这样您就不会在代码中使用完全限定的名称,例如System.IO.FileInfo
。最后,对象初始值设定项对于类(如ProcessStartInfo
)很有用:
var startInfo = new ProcessStartInfo(path) {
CreateNoWindow = false,
UseShellExecute = true,
WorkingDirectory = new FileInfo(path).DirectoryName;
};
var process = Process.Start(startInfo);
编辑:经过一定程度的协作后:)
问题在于,这是在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);
您正在创建一个进程startinfo
,但随后完全忽略它。您应该将其传递到进程。启动。您还应该重命名变量。传统上,局部变量以C#中的小写字母开始。此外,在可能的情况下,最好在首次使用时初始化变量。哦,导入名称空间,这样您就不会在代码中使用完全限定的名称,例如System.IO.FileInfo
。最后,对象初始值设定项对于类(如ProcessStartInfo
)很有用:
var startInfo = new ProcessStartInfo(path) {
CreateNoWindow = false,
UseShellExecute = true,
WorkingDirectory = new FileInfo(path).DirectoryName;
};
var process = Process.Start(startInfo);
谢谢你的建设性批评,但是你知道问题的解决方案吗?@LajosArpad:你试过修改过的代码吗?我希望它会起作用-您需要输入到ProcessStartInfo
中的信息,但是您忽略了它,所以当您只调用Process.Start(path)
时,您不会得到这些信息。这就是问题的解决方案——我也给了你一些代码卫生技巧。不,它不执行NuGet命令。我用新的C代码编辑了我的问题。@LajosArpad:那会发生什么?你看到错误了吗?也许这是nuget的路径问题?(如果在批处理文件中完全限定,会发生什么情况?)。是的,我已经测试了路径。我想可能缺少一个设置,但我不知道这种行为的真正原因。谢谢你的建设性批评,但是你知道问题的解决方案吗?@LajosArpad:你试过修改过的代码吗?我希望它会起作用-您需要输入到ProcessStartInfo
中的信息,但是您忽略了它,所以当您只调用Process.Start(path)
时,您不会得到这些信息。这就是问题的解决方案——我也给了你一些代码卫生技巧。不,它不执行NuGet命令。我用新的C代码编辑了我的问题。@LajosArpad:那会发生什么?你看到错误了吗?也许这是nuget的路径问题?(如果在批处理文件中完全限定,会发生什么情况?)。是的,我已经测试了路径。我想可能缺少一个设置,但我不知道这种行为的真正原因。