如何通过Windows窗体应用程序安装另一个应用程序?我有要安装的应用程序的安装文件(* .exe)。我希望该过程以静默方式运行,并在成功完成时显示一个MessageBox。
下面是我尝试过的代码:
private void button1_Click(object sender, EventArgs e)
{
if(checkBox1.Checked == true)
{
//samplep = Process.Start(@"run\msiexec.exe", "/norestart /i setup.msi");
// Part 1: use ProcessStartInfo class.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = @"E:\Test projects\desktop\dd\dd\Data\wrar521b1.exe"; //Enter the path to the setup file
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
// Part 2: set arguments.
startInfo.Arguments = "-f j -o \"" + e + "\" -z 1.0 -s y " + e;
try
{
// Part 3: start with the info we specified.
// ... Call WaitForExit.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
MessageBox.Show("Error", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
}
}
}
如果您具有要安装的应用程序的安装文件(.exe),则可以通过以下方式轻松完成:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "Enter filepath here";//Enter the path to your setup file
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
var process = new Process() ;
process.StartInfo = psi;
var handle = Process.GetCurrentProcess().MainWindowHandle;
Process.Start(psi).WaitForInputIdle();
SetForegroundWindow(handle.ToInt32());
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.WaitForExit();
MessageBox.Show("Setup finished!");
此Google搜索可能会有用:如何静默运行exe C#
编辑:
PowerShell powerShell = null;
try
{
using (powerShell = PowerShell.Create())
{
//Enter the path to your setup file instead of 'filepath'
powerShell.AddScript("$setup=Start-Process 'filepath ' -ArgumentList ' / S ' -Wait -PassThru");
Collection<PSObject> PSOutput = powerShell.Invoke(); foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{
//MessageBox.Show(outputItem.BaseObject.GetType().FullName);
//MessageBox.Show(outputItem.BaseObject.ToString() + "\n");
}
}
if (powerShell.Streams.Error.Count > 0)
{
string temp = powerShell.Streams.Error.First().ToString();
}
else
{
MessageBox.Show("Setup finished!")
}
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
finally
{
if (powerShell != null)
powerShell.Dispose();
}