I am making an installer for one of my applications, whenever I hit install and I get to the 'folder already exists' message box I made I click OK but then a new process of my application appears and I don't want it to! How do I fix this? Here's my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace GladeInstaller
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
ProcessStartInfo startInfo;
startInfo = new ProcessStartInfo();
startInfo.FileName = Path.Combine
(Path.GetDirectoryName(Application.ExecutablePath), "GladeInstaller.exe");
startInfo.Arguments =
string.Empty;
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
Process.Start(startInfo);
}
catch (Exception)
{
MessageBox.Show("The installer needs to be ran as administraitor in order for it to work, if you dont have theese priverlages download Glade Skinned", "Glade Installation Error");
Environment.Exit(0);
}
string path = @"c:\Glade";
try
{
// Determine whether the directory exists.
if (Directory.Exists(path))
{
MessageBox.Show("The directory C:\\Glade allready exists! Delecte the folder C:\\Glade and re-run the installer");
Application.Exit();
}
DirectoryInfo di = Directory.CreateDirectory(path);
Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));
}
catch (Exception ec)
{
Console.WriteLine("The process failed: {0}", ec.ToString());
try
{
foreach (Process proc in Process.GetProcessesByName("Glade Installer"))
{
proc.Kill();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Environment.Exit(0);
}
try
{
foreach (Process proc in Process.GetProcessesByName("Glade Installer"))
{
proc.Kill();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
MessageBox.Show("Installation finished!", "Glade Installer");
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
All the code is correct, sorry if it isn't formatted correctly.
Edit:
After Process.Start(startInfo)
I realised that I need to put Application.Exit()