我正在使用Process
执行一个批处理文件,该文件将生成证书文件。
当我执行其他文件(其中包含openssl命令(时,代码工作得很好。但当我执行一个包含keytool命令的文件时,它执行了,但没有生成任何文件。
我已经:
- 将
UseShellExecute
设置为true - 设置
WaitForExit(-1)
并发现返回为true,所以它确实执行了 - 我手动单击了该批处理文件,该文件立即生成,因此命令很好:(
- 顺便说一句,我使用的是.Net Core MVC
我在任何地方都找不到任何错误代码,所以我现在束手无策。
有人知道线索吗?如果有任何帮助,我们将不胜感激!
成功代码(openssl(:
我首先在该文件夹中生成一个p12文件(证书格式(,它运行良好。
private string Gen_P12(string domain, string pwd)
{
//generate folder
string folder = @"D:Temp";
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
//generate bat(p12)
string bat = "openssl.exe pkcs12 -export -inkey " + domain + ".key -in " + domain + ".cer -out " + domain + ".p12 -password pass:" + pwd +"rn";
//download in folder
var path = Path.Combine(folder, domain + "_P12.bat");
using (FileStream fs = System.IO.File.Create(path))
{
byte[] content = new UTF8Encoding(true).GetBytes(bat);
fs.Write(content, 0, content.Length);
}
Thread.Sleep(500);
//execute
ProcessStartInfo myBat = new ProcessStartInfo();
string name = domain + "_P12.bat";
myBat.FileName = name;
myBat.WorkingDirectory = folder;
myBat.UseShellExecute = true;
//Process.Start(myBat);
Process p = Process.Start(myBat);
p.WaitForExit(-1);
return folder;
}
失败代码(keytool(:
尝试使用P12文件和keytool命令来生成密钥库(也是证书格式(,但失败了。
private string Gen_KS(string domain, string folder, string CA_domain, byte[] cer, string pwd)
{
//generate bat
string bat = "keytool -importkeystore -srckeystore " + domain + ".p12 -srcstoretype PKCS12 -srcstorepass " + pwd + " -destkeystore " + domain + ".keystore -storepass " + pwd + "rn";
var path = Path.Combine(folder, domain + "_KS.bat");
using (FileStream fs = System.IO.File.Create(path))
{
byte[] content = new UTF8Encoding(true).GetBytes(bat);
fs.Write(content, 0, content.Length);
}
Thread.Sleep(700);
//execute
ProcessStartInfo myBat = new ProcessStartInfo();
myBat.WorkingDirectory = folder;
string name = domain + "_KS.bat";
myBat.FileName = name;
myBat.UseShellExecute = true;
Process p = Process.Start(myBat);
var a = p.WaitForExit(-1);
string route = folder + domain + ".keystore";
return route;
}
谢谢!
感谢@user9938,我解决了这个问题!
1.简短结论:我需要以管理员身份处理bat。
(我仍然不明白为什么只有keytool
命令需要管理员权限(
2.查找错误:(UseShellExecute=true时如何应用StanderError(
事实上,我们不必将其设置为true来执行命令。试试这个(替换执行部分(:
Process process = new Process();
try
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = "cmd.exe";
process.Start();
process.StandardInput.WriteLine(bat); //command string, not the bat file
process.StandardInput.AutoFlush = true;
process.StandardInput.WriteLine("exit");
StreamReader reader = process.StandardError;
string curLine = reader.ReadLine();
reader.Close();
process.WaitForExit();
process.Close();
}catch (Exception e){}
通过Breakpoints检查curLine
的值,错误消息为:"keytool’不被识别为内部或外部命令、可操作程序或批处理文件";。
3.如何解决:
只需将CCD_ 6属性设置为";runas";。
//execute
ProcessStartInfo myBat = new ProcessStartInfo();
myBat.WorkingDirectory = folder;
string name = domain + "_KS.bat";
myBat.Verb = "runas";
myBat.FileName = name;
myBat.UseShellExecute = true;
Process p = Process.Start(myBat);
var a = p.WaitForExit(-1);
完成!感谢用户9938<3