0

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()

  • It would be helpful if you provided an image of your form and also what buttons are bound to button1_Click and button2_Click – Ryan Wilson Mar 05 '18 at 16:57
  • `a new process of my application appears and I don't want it to` - then don't launch a new process with `Process.Start` from `button1_Click`? – GSerg Mar 05 '18 at 16:57
  • Are you meaning to do `Application.Exit()`, or do you mean to have `Environment.Exit(0);` after your message box? – Ryan C Mar 05 '18 at 16:58
  • https://stackoverflow.com/questions/1057151/application-exit#1057159 – Phiter Mar 05 '18 at 16:58
  • @GSerg The Process.Start is for loading an adminastrive coppy of the window –  Mar 05 '18 at 17:24
  • `button2` is just a button for exiting the form, just ignore it. –  Mar 05 '18 at 17:30

2 Answers2

0

Application.Exit() kills Application.

Here i refactored your code to stop doing that please try it.

        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");

                // This part was killing you application 
                // now it just ends click event without killing application.

                //Application.Exit();
            }
            else
            {
                DirectoryInfo di = Directory.CreateDirectory(path);
                Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));
                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();
            }
        }
        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);
        }
Mihir Dave
  • 3,954
  • 1
  • 12
  • 28
0

You are starting a new process in Button1_Click. Take that out and it won't loop. If you need to run the installer as administrator, do it by adding the following to the application manifest file:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Your user will be prompted via the UAC to continue.

You should also make sure the user has UAC enabled. If they don't and they are not running as an admin you should provide an error message indicating that.