Renish Nair
Renish Nair

Reputation: 45

Not able to get Application startup path on click on setup.exe

I am publishing a Windows project and on click on form I am installing another setup to install.

I am not getting the current Application Startup Path on clickevent on button.

On debug and Release it is showing the right path but after publishing it is giving

C:\Users\username\AppData\Local\Apps\2.0 path

Already I have used :

Application.StartupPath
Application.Executablepath
Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location))
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase))
Path.Combine(Directory.GetCurrentDirectory())

But of no use it always show

C:\Users\username\AppData\Local\Apps\2.0 path

Upvotes: 2

Views: 1836

Answers (2)

Nasreddine
Nasreddine

Reputation: 37908

You are getting that path because it's the one used by ClickOnce. ClickOnce applications are installed under the profile of the user who installed them.

Edit :

Method 1:

Here's a way to get the path where your application was installed from (works only if your application was installed) (parts of this were written by @codeConcussion) :

// productName is name you assigned to your app in the 
// Project properties -> Publish -> Publish Settings
public static string GetInstalledFromDir(string productName)
{
    using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall"))
    {
        if (key != null)
        {
            var appKey = key.GetSubKeyNames().FirstOrDefault(x => GetValue(key, x, "DisplayName") == productName);
            return appKey == null ? null : GetValue(key, appKey, "UrlUpdateInfo");
        }
    }

    return null;
}

private static string GetValue(RegistryKey key, string app, string value)
{
    using (var subKey = key.OpenSubKey(app))
    {
        if (subKey == null || !subKey.GetValueNames().Contains(value)) 
        { 
            return null; 
        }

        return subKey.GetValue(value).ToString();
    }
}

Here's how to use it :

Uri uri = new Uri(GetInstalledFromDir("ProductName"));
MessageBox.Show(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)));

Method 2 :

You can also try

System.Deployment.Application.ApplicationDeployment.CurrentDeployment.ActivationUri

But I think this one works only if your app was installed from the internet

Upvotes: 3

Jarek Kardas
Jarek Kardas

Reputation: 8455

try this:

Process.GetCurrentProcess().MainModule.FileName

BTW, is it ClickOnce deployment? If so then the directory you are getting looks about right.

Upvotes: 0

Related Questions