50% OFF!!!

Showing posts with label windows 7. Show all posts
Showing posts with label windows 7. Show all posts

Wednesday, December 8, 2010

C# | Check if app is installed for “All users” or "Just me"


Today I bring a very HARD-to-find topic about getting an installed application "Installation context".
An installation context may be Per-Machine Installation Context (ALLUSERS=1) and Per-User Installation Context (ALLUSERS="").

When installing an application, the user may select one of two options:
a. Install [APP] for yourself
b. or for anyone who uses this computer

[a] installs only for the current user(just me) while [b] installs for the Local System user which is for all-users(everyone).

In this post I will give a code for checking is an installed application was installed for "Everyone" or "Just me".

The validation process is check if the APPLICATION-NAME exists in the registry keys under UserData.
(*) Everyone - check under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\ (S-1-5-18 represents the Local System user)

(*) Just me (Current user) - check under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-?!?!?!?!\Products\ (S-?!?!?!?! represents the Current user. this value found by the code: WindowsIdentity.GetCurrent().User.Value.


[Code] Method's return type:
private enum InstallationContexts { NotInstalled, Everyone, JustMe };

[Code] Method declaration:
private InstallationContexts GetInstalledContext(string p_appDisplayName)
{
    //The S-1-5-18  =
Local System user (Everyone) | HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\

    //The S-XXXXXX  =
Current user (Just me)       |
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-?!?!?!?!\Products\

    string key;
    string keyFormat = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\{0}\Products\";
 
    // Check if installed for -> Everyone
    key = string.Format(keyFormat, "S-1-5-18");
    bool res = GetInstalledContext_IsRegKeyExists(key, p_appDisplayName, StringComparison.OrdinalIgnoreCase);
    if (res == true)
    {
        return InstallationContexts.Everyone;
    }
 
    // Check if installed for -> Just me
    key = string.Format(keyFormat, WindowsIdentity.GetCurrent().User.Value);
    res = GetInstalledContext_IsRegKeyExists(key, p_appDisplayName, StringComparison.OrdinalIgnoreCase);
    if (res == true)
    {
        return InstallationContexts.JustMe;
    }
 
    return InstallationContexts.NotInstalled;
}
private bool GetInstalledContext_IsRegKeyExists(string p_regKey, string p_appDisplayName, StringComparison p_scompare)
{
    using (RegistryKey regkey = Registry.LocalMachine.OpenSubKey(p_regKey))
    {
        if (regkey != null)
        {
            RegistryKey rk;
            string[] arrProducs = regkey.GetSubKeyNames();
            for (int i = 0; i < arrProducs.Length; i++)
            {

               using (rk = regkey.OpenSubKey(arrProducs[i] + @"\InstallProperties"))
               {
if (rk != null &&
                        p_appDisplayName.Equals(rk.GetValue("DisplayName").ToString(), p_scompare) == true)
                   {
                        return true;
                   }
               }
            }
        }
    }
 
    return false;
}

[Code] Method's usage example:
InstallationContexts appContext = GetInstalledContext("APP-DISPLAY-NAME");

This code gives the ability to detect previously installed application or previously installed software is installed to ALLUSERS (everyone) or only to CURRENT USER (just me).



Addition on 19.12.2010:
(*) This checking operation is valid ONLY for an application and software which were installed using Microsoft's Windows Installer.
For general checking for installed application, check my post about check if program/application is installed.


Enjoy...
MDB-Blog

Tuesday, November 23, 2010

How to determine the Windows version by using Visual C# | windows 7 windows vista

Here is a good code includes Windows 7 and Windows Vista recognaizer.

Code:
 
 
 
 
internal enum WindowsVersions { UnKnown, Win95, Win98, WinMe, WinNT3or4, Win2000, WinXP, WinServer2003, WinVista, Win7, Win8, MacOSX, Unix, Xbox };
internal static WindowsVersions GetCurrentWindowsVersion()
{
    // Get OperatingSystem information from the system namespace.
    System.OperatingSystem osInfo = System.Environment.OSVersion;
 
    // Determine the platform.
    if (osInfo.Platform == System.PlatformID.Win32Windows)
    {
        // Platform is Windows 95, Windows 98, Windows 98 Second Edition, or Windows Me.
        switch (osInfo.Version.Minor)
        {
            case 0:
                //Console.WriteLine("Windows 95");
                return WindowsVersions.Win95;
 
            case 10:
                //if (osInfo.Version.Revision.ToString() == "2222A")
                //    Console.WriteLine("Windows 98 Second Edition");
                //else
                //    Console.WriteLine("Windows 98");
                return WindowsVersions.Win98;
 
            case 90:
                //Console.WriteLine("Windows Me");
                return WindowsVersions.WinMe;
        }
    }
    else if (osInfo.Platform == System.PlatformID.Win32NT)
    {
        // Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000, or Windows XP.
        switch (osInfo.Version.Major)
        {
            case 3:
            case 4:
                //Console.WriteLine("Windows NT 3.51"); // = 3
                //Console.WriteLine("Windows NT 4.0");  // = 4
                return WindowsVersions.WinNT3or4;
 
            case 5:
                switch (osInfo.Version.Minor)
                {
                    case 0:
                        //name = "Windows 2000";
                        return WindowsVersions.Win2000;
                    case 1:
                        //name = "Windows XP";
                        return WindowsVersions.WinXP;
                    case 2:
                        //name = "Windows Server 2003";
                        return WindowsVersions.WinServer2003;
                }
                break;
 
            case 6:
                switch (osInfo.Version.Minor)
                {
                    case 0:
                        // Windows Vista or Windows Server 2008 (distinct by rpoduct type)
                        return WindowsVersions.WinVista;
 
                    case 1:
                        return WindowsVersions.Win7;
 
                    case 2:
                        return WindowsVersions.Win8;
                }
                break;
        }
    }
    else if (osInfo.Platform == System.PlatformID.Unix)
    {
        return WindowsVersions.Unix;
    }
    else if (osInfo.Platform == System.PlatformID.MacOSX)
    {
        return WindowsVersions.MacOSX;
    }
    else if (osInfo.Platform == PlatformID.Xbox)
    {
        return WindowsVersions.Xbox;
    }
    return WindowsVersions.UnKnown;
}
 
 
 
 
 
2013/07/02: added WIN8 support