C# 检测某版本VC++是否安装
阅读原文时间:2023年07月11日阅读:1

一、根据微软的产品号GUID,调用msi.dll

class Program  
{  
    static void Main(string\[\] args)  
    {  
        if (GetProcuct("{GUID}") == INSTALLSTATE.INSTALLSTATE\_DEFAULT)  
        {  
            //操作  
        }  
    }  
    \[DllImport("msi.dll")\]  
    private static extern INSTALLSTATE MsiQueryProductState(string product);  
    public static INSTALLSTATE GetProcuct(string product)  
    {  
        INSTALLSTATE state = MsiQueryProductState(product);  
        return state;  
    }  
}

public enum INSTALLSTATE  
{  
    INSTALLSTATE\_NOTUSED = -7,  // component disabled  
    INSTALLSTATE\_BADCONFIG = -6,  // configuration data corrupt  
    INSTALLSTATE\_INCOMPLETE = -5,  // installation suspended or in progress  
    INSTALLSTATE\_SOURCEABSENT = -4,  // run from source, source is unavailable  
    INSTALLSTATE\_MOREDATA = -3,  // return buffer overflow  
    INSTALLSTATE\_INVALIDARG = -2,  // invalid function argument  
    INSTALLSTATE\_UNKNOWN = -1,  // unrecognized product or feature  
    INSTALLSTATE\_BROKEN = 0,  // broken  
    INSTALLSTATE\_ADVERTISED = 1,  // advertised feature  
    INSTALLSTATE\_REMOVED = 1,  // component being removed (action state, not settable)  
    INSTALLSTATE\_ABSENT = 2,  // uninstalled (or action state absent but clients remain)  
    INSTALLSTATE\_LOCAL = 3,  // installed on local drive  
    INSTALLSTATE\_SOURCE = 4,  // run from source, CD or net  
    INSTALLSTATE\_DEFAULT = 5,  // use default, local or source  
}

调用 MsiQueryProductState (函数详细:https://docs.microsoft.com/zh-cn/windows/win32/api/msi/nf-msi-msiqueryproductstatea?redirectedfrom=MSDN)函数,来查看VC++是否安装,该方式需要自定义一个枚举 INSTALLSTATE,还需要知道VC++软件的GUID,缺陷也是如果不知道GUID就难以使用。

   Visual C++ 2005 Redistributable Package (x86)
{A49F249F-0C91-497F-86DF-B2585E8E76B7}
Visual C++ 2005 Redistributable Package (x64)
{6E8E85E8-CE4B-4FF5-91F7-04999C9FAE6A}
Visual C++ 2005 Redistributable Package (ia64)
{03ED71EA-F531-4927-AABD-1C31BCE8E187}
Visual C++ 2005 SP1 Redistributable Package (x86)
{7299052B-02A4-4627-81F2-1818DA5D550D}
Visual C++ 2005 SP1 Redistributable Package (x64)
{071C9B48-7C32-4621-A0AC-3F809523288F}
Visual C++ 2005 SP1 Redistributable Package (ia64)
{0F8FB34E-675E-42ED-850B-29D98C2ECE08}
Visual C++ 2008 Redistributable Package (x86)
{FF66E9F6-83E7-3A3E-AF14-8DE9A809A6A4}
Visual C++ 2008 Redistributable Package (x64)
{350AA351-21FA-3270-8B7A-835434E766AD}
Visual C++ 2008 Redistributable Package (ia64)
{2B547B43-DB50-3139-9EBE-37D419E0F5FA}
Visual C++ 2008 SP1 Redistributable Package (x86)
{9A25302D-30C0-39D9-BD6F-21E6EC160475}
Visual C++ 2008 SP1 Redistributable Package (x86 9.0.30729.6161)
{9BE518E6-ECC6-35A9-88E4-87755C07200F}
Visual C++ 2008 SP1 Redistributable Package (x64)
{8220EEFE-38CD-377E-8595-13398D740ACE}
Visual C++ 2008 SP1 Redistributable Package (ia64)
{5827ECE1-AEB0-328E-B813-6FC68622C1F9}
Visual C++ 2010 Redistributable Package (x86)
{196BB40D-1578-3D01-B289-BEFC77A11A1E}
Visual C++ 2010 Redistributable Package (x64)
{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}
Visual C++ 2010 Redistributable Package (ia64)
{C1A35166-4301-38E9-BA67-02823AD72A1B}
Visual C++ 2010 SP1 Redistributable Package (x86 10.0.40219)
{F0C3E5D1-1ADE-321E-8167-68EF0DE699A5}
Visual C++ 2010 SP1 Redistributable Package (x64)
{1D8E6291-B0D5-35EC-8441-6616F567A0F7}
Visual C++ 2010 SP1 Redistributable Package (ia64)
{88C73C1C-2DE5-3B01-AFB8-B46EF4AB41CD}
Visual C++ 2013 Redistributable Package (x86 12.0.21005)
{13A4EE12-23EA-3371-91EE-EFB36DDFFF3E} and {F8CFEB22-A2E7-3971-9EDA-4B11EDEFC185}
Visual C++ 2013 Redistributable Package (x64)
{929FBD26-9020-399B-9A7A-751D61F0B942} and {A749D8E6-B613-3BE3-8F5F-045C84EBA29B}

(参考:https://www.cnblogs.com/yidanda888/archive/2004/01/13/11987411.html)

二、查询本地注册表:“SOFTWARE\WOW6432Node\Microsoft\VisualStudio”。这里以查询VC++2015为例

    public static string GetProcuct()  
    {  
        RegistryKey key = Registry.LocalMachine;  
        RegistryKey rkOpen = key.OpenSubKey("SOFTWARE\\\\WOW6432Node\\\\Microsoft\\\\VisualStudio\\\\14.0\\\\VC\\\\Runtimes\\\\x64", false);  
        if (rkOpen.GetValue("Version") == null)  
        {  
            return null;  
        }  
        return rkOpen.GetValue("Version").ToString();  
    }

缺陷是当安装VC++2017时,将会替换掉该注册表里的信息。https://developercommunity.visualstudio.com/content/problem/332815/visual-c-redistributable-2017-install-removes-visu.html

(参考:https://www.cnblogs.com/liujx2019/p/10307520.html)

三、查询注册表的删除目录,因为如果安装VC++就会在删除目录里存在。

    public static void GetProcucts()  
    {  
        List<string> Lists = new List<string>();  
        RegistryKey key = Registry.LocalMachine;  
        GetRegistry(key, @"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall", ref Lists);  
        if (Lists.Count > 0)  
        {if (!string.IsNullOrEmpty(Lists.FirstOrDefault(x => x.Contains("2015"))))  
            {  
                //操作  
            }  
        }  
    }  
    private static void GetRegistry(RegistryKey keyR, String str, ref List<string> list)  
    {

        string\[\] subkeyNames;  
        string\[\] subvalueNames;  
        RegistryKey aimdir = keyR.OpenSubKey(str);  
        if (aimdir != null)  
        {  
            subvalueNames = aimdir.GetValueNames();  
            foreach (string valueName in subvalueNames)  
            {  
                if (valueName.ToLower().Equals("displayname") || valueName.ToLower().Equals("productname"))  
                {  
                    if (aimdir.GetValue(valueName) != null &&  
                        aimdir.GetValue(valueName).ToString().ToLower().Contains("microsoft visual c++ 20") &&  
                        aimdir.GetValue(valueName).ToString().ToLower().Contains("redistributable"))  
                    {  
                        list.Add(aimdir.GetValue(valueName).ToString());  
                    }  
                }  
            }  
            subkeyNames = aimdir.GetSubKeyNames();  
            foreach (string keyName in subkeyNames)  
            {  
                GetRegistry(aimdir, keyName, ref list);  
            }  
        }  
    }

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章