2

I'm currently working on an Autoupdater for my Applications. (combined with an installer, updater and removal tool)

I'd like to setup all the Stuff automatically:

  • Startmenu Entry
  • Folder in C:\Program Files\
  • Config Folder in App Data (depending on Roaming / local)
  • Software Removal-Entry in "Programs & Features"
  • Desktop Shortcut.

This works all very well. For writing to Program Files and the registry the tool needs Administrator privilegs ofc. So I added a "restart" of the AppLauncher, after the desired software has been picked. Just something like this:

ProcessStartInfo pi = new ProcessStartInfo(Directory.GetCurrentDirectory() + @"\AppLauncher.exe");
        pi.Verb = "runas";
        pi.Arguments = "install " + this.appItem.APID;

        Process p = new Process();
        p.StartInfo = pi;
        try
        {
            p.Start();
            Application application = Application.Current;
            application.Shutdown();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to install the application.\n\n" + ex.ToString(), "Error", 
            application.Shutdown();
        }

Also this step works very well, if the User saves the AppLauncher.exe somewhere in a Folder and starts it by double clicking it.

If a User decides to hit "run" from the Browser, The installer runs into the shown exception, saying "AppLauncher.exe" not found. I Assume, that starting the File right from the browser will setup a different WorkingDirectory and therefore

Directory.GetCurrentDirectory()

will not return the appropriate value, where the file is located. It may also NOT work, if the user decides to give the file another name.

So, what can I do on this?

Is there something like File.GetCurrentFile() :P

dognose
  • 20,360
  • 9
  • 61
  • 107

1 Answers1

5

Replace

Directory.GetCurrentDirectory() + @"\AppLauncher.exe"

with

System.Reflection.Assembly.GetEntryAssembly().Location

(if you

However, it is better not to reinvent the bicycle but to use one of the available installer solutions which have already implemented all the integration features and already solved all the bugs.

For example: ClickOnce, WiX.

penartur
  • 9,792
  • 5
  • 39
  • 50
  • Isn't leveraging Reflection a little overkill for this purpose ? – Alex Jul 03 '12 at 08:51
  • It comes built-in with .NET, so I doubt it will have some effect on performance. OP wants to get the currently executing program path; that's what `GetEntryAssembly()` was intended for. PS: Looking at the question more carefully, it is exactly what OP is looking for ("`File.GetCurrentFile()`"). – penartur Jul 03 '12 at 08:53
  • Actually, the assembly containing the currently executing code is retrieved via `System.Reflection.Assembly.GetExecutingAssembly()` – Alex Jul 03 '12 at 08:57
  • @Alex OP wants to know the path to their `AppLauncher.exe`, not to .dll which contains the currently executing code. – penartur Jul 03 '12 at 08:59
  • +1 because it's correct and for suggesting not to reinvent the wheel :) – Maghis Jul 03 '12 at 09:13
  • Thx a lot so far. Will try that immediate. I know, that there are a lot of installers out there. But since I'm using a "self-made" dll framework, that all my applications share (located in program files/myName/common, not program files/myName/certainApp), no installer really mets my requirements. – dognose Jul 03 '12 at 11:06