我正在研究C#控制台应用程序。它只有一个要求。它应该静默安装.exe安装文件。这些安装文件是从InstallShield或其他产品生成的。我没有生成任何设置文件。我尝试使用此代码安装Notepad ++,但它并不是真的。
为了更具体,并避免重复标记这个问题,我已经提到了很多相关的链接。他们之中有一些是
我试过的代码:
选项1
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = "/s /v /qn /min";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = newRenamedFile;
psi.UseShellExecute = false;
Process.Start(psi);
OPTION-2
executableFilePath
是.exe安装文件的路径。
public static void DeployApplications(string executableFilePath)
{
PowerShell powerShell = null;
Console.WriteLine(" ");
Console.WriteLine("Deploying application...");
try
{
using (powerShell = PowerShell.Create())
{
powerShell.AddScript("$setup=Start-Process '" + executableFilePath + "' -ArgumentList '/s /v /qn /min' -Wait -PassThru");
Collection<PSObject> PSOutput = powerShell.Invoke();
foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{
Console.WriteLine(outputItem.BaseObject.GetType().FullName);
Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
}
}
if (powerShell.Streams.Error.Count > 0)
{
string temp = powerShell.Streams.Error.First().ToString();
Console.WriteLine("Error: {0}", temp);
}
else
Console.WriteLine("Installation has completed successfully.");
}
}
catch (Exception ex)
{
Console.WriteLine("Error occured: {0}", ex.InnerException);
}
finally
{
if (powerShell != null)
powerShell.Dispose();
}
}
OPTION-1和OPTION-2都打开了设置向导UI。没有安装过程启动,并且计算机上未安装任何应用程序。执行此代码时没有错误。
质询
输出我想要
控制台应用程序必须静默安装.exe文件。必须跳过安装向导UI。如果出现控制台窗口,则可以。我不希望任何UI出现在控制台窗口之外。
最后,我找到了解决方案并解决了我的要求。
需要使用以下选项将app.manifest文件添加到WPF应用程序。应用必须以管理员权限打开。
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
更改静默安装的脚本。
private void deployApplications(string executableFilePath)
{
PowerShell powerShell = null;
Console.WriteLine(" ");
Console.WriteLine("Deploying application...");
try
{
using (powerShell = PowerShell.Create())
{
powerShell.AddScript(executableFilePath + " /S /v/qn");
Collection<PSObject> PSOutput = powerShell.Invoke();
foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{
Console.WriteLine(outputItem.BaseObject.GetType().FullName);
Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
}
}
if (powerShell.Streams.Error.Count > 0)
{
string temp = powerShell.Streams.Error.First().ToString();
Console.WriteLine("Error: {0}", temp);
}
else
Console.WriteLine("Installation has completed successfully.");
}
}
catch (Exception ex)
{
Console.WriteLine("Error occured: {0}", ex.InnerException);
//throw;
}
finally
{
if (powerShell != null)
powerShell.Dispose();
}
}