使用Process类调用EXE程序出错的问题

    
Process process = new Process();
string strSignToolPath = string.Empty;
string strPfxFilePath = string.Empty;
string password = string.Empty;
string strFilePath = string.Empty;
string strCmd = string.Empty;
string strRst = string.Empty;
try
{
    process.StartInfo.FileName 
= "cmd.exe";
    process.StartInfo.UseShellExecute 
= false;
    process.StartInfo.RedirectStandardInput 
= true;
    process.StartInfo.RedirectStandardOutput 
= true;
    process.StartInfo.RedirectStandardError 
= true;
    process.StartInfo.CreateNoWindow 
= true;
    process.Start();
    process.StandardInput.AutoFlush 
= true;
    strCmd 
= strSignToolPath + @" sign /f " + strPfxFilePath + " /p " + password + " " + strFilePath;
    process.StandardInput.WriteLine(strCmd);
    process.StandardInput.WriteLine(
"exit");
    strRst 
= process.StandardOutput.ReadToEnd();
    process.Close();
}

catch (Exception ex)
{
    
return ex.Message;
}

上边这段代码的目的是调用.net framework里面的signtool.exe工具去给一个文件进行签名。strSignToolPath是signtool.exe工具所在的路径,strPfxFilePath是签名是需要的pfx文件的路径,password是pfx文件的密码,strFilePath是要签名的文件所在的路径。当这段代码在Console或Winform类型的项目中运行时,可以对文件进行正确签名,但是当运行在web类型的项目中时,却不能对文件进行正确签名。
经过查找,发现原因是用户权限问题,当运行在Console或Winform类型项目中时,默认用户权限比运行在web类型项目中时高。解决办法是把访问web项目的用户权限置为Administrator。 设置用户权限的方法:XP系统中,Control Pannel-User Accounts中设置;win2003系统中,Administrative Tools-Computer Management-System Tools-Local Users and Groups-Groups中设置。 如果还是不能正常签名,则可以右键单击要签名的文件所在的文件夹,选择属性打开属性对话框,转到安全页,把NETWORK SERVICE用户添加到用户列表中,并把它的权限设为完全控制。
原文地址:https://www.cnblogs.com/pdfw/p/918066.html