我正在使用软件包安装程序工具,该工具需要从该工具安装应用程序而无需用户干预,并且应该在模拟了管理员帐户后开始安装。 这是我当前正在使用的代码。但是,无需进行静默安装,它甚至不会冒充管理员。有人可以通过模拟该WinForms C#应用程序来帮助我实现无提示安装吗?
private void SilentInstaller(string dir, string package, string option, long PckgID)
{
if (File.Exists(dir + @"\" + package))
{
Pckg_Ws_ExecModel obj = PendingPackages.Where(x => x.Pckg_ID == PckgID).FirstOrDefault();
if (obj.Status != "Installing" || obj.Status != "Installed")
{
obj.Status = "Installing";
packageWS_service.UpdatePackageWorkstation(obj);
SafeTokenHandle safeTokenHandle;
string domainName = "corp";
string userName = ConfigurationManager.AppSettings["xusername"];
string password = ConfigurationManager.AppSettings["xpassword"];
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
try
{
Console.WriteLine("Started myProcess");
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(userName, domainName, password,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
out safeTokenHandle);
if (returnValue)
{
using (safeTokenHandle)
{
//if (processingPackage.Pckg_ID > 0)
// AddEvent(processingPackage.Pckg_ID, "Download Started");
Console.WriteLine("Before impersonation: " + WindowsIdentity.GetCurrent().Name);
using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
{
using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
{
Console.WriteLine("After impersonation: " + WindowsIdentity.GetCurrent().Name + "\n");
Process myProcess = new Process();
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = dir + @"\" + package;
myProcess.StartInfo.Arguments = "/user:Administrator \"cmd /K " + "" + "\"";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
myProcess.WaitForExit();
}
}
}
}
Console.WriteLine("Prog should have waited for exit to print this");
//MSGError("Installation completed");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
//MSGError("Installation failed");
//MSGError(e.ToString());
errorService.AddError(new ErrorModel() { Ws_ID = MachineName(), Usr = UserName(), Pckg_ID = PckgID, Error1 = e.Message });
}
}
}
else
{
//Errors.Detect = true;
//MSGError("Installer no longer exist");
errorService.AddError(new ErrorModel() { Ws_ID = MachineName(), Usr = UserName(), Pckg_ID = PckgID, Error1 = "Installer no longer exist" });
}
}