c# 判斷office是否安裝 C#判斷office版本


因為程序需要判斷office2010版本以上才可以使,網上找了好多檢測office版本都不能用,

參考https://jingyan.baidu.com/article/2c8c281d0844084008252a8f.html

根據注冊表信息檢查,代碼改進了一下office2007 2010,2013,2016都能檢測到

public void checkOffice()
        {
            bool ifused = false;
            int officeVersion = 0;

            RegistryKey rk = Registry.LocalMachine;
            RegistryKey akey07 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\12.0\Excel\InstallRoot\");//查詢2007
            RegistryKey akey10 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\14.0\Excel\InstallRoot\");//查詢2010
            RegistryKey akey13 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\15.0\Excel\InstallRoot\");//查詢2013
            RegistryKey akey16 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\16.0\Excel\InstallRoot\");//查詢2016

            //檢查本機是否安裝Office2007
            if (akey07 != null)
            {
                string office07 = akey07.GetValue("Path").ToString();
                if (File.Exists(office07 + "Excel.exe"))
                {
                    ifused = true;
                    officeVersion = 2007;
                }
            }

            //檢查本機是否安裝Office2010
            if (akey10 != null)
            {
                string office10 = akey10.GetValue("Path").ToString();
                if (File.Exists(office10 + "Excel.exe"))
                {
                    ifused = true;
                    officeVersion = 2010;
                }
            }

            //檢查本機是否安裝Office2013
            if (akey13 != null)
            {
                string office13 = akey13.GetValue("Path").ToString();
                if (File.Exists(office13 + "Excel.exe"))
                {
                    ifused = true;
                    officeVersion = 2013;
                }

            }

            //檢查本機是否安裝Office2016       
            if (akey16 != null)
            {
                string office16 = akey16.GetValue("Path").ToString();
                if (File.Exists(office16 + "Excel.exe"))
                {
                    ifused = true;
                    officeVersion = 2016;
                }
            }


            Debug.WriteLine("result:"+ ifused.ToString());
            Debug.WriteLine("office:" + officeVersion.ToString());
        }

debug調試結果:
result:True
office:2016


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM