private static string GetMSOfficeApplicationName(MSOfficeApplication name) { foreach (MSOfficeApplication enumType in Enum.GetValues(typeof(MSOfficeApplication))) { if (enumType == name) { return(enumType.ToString()); } } return(null); }
/// <summary> /// 取得 Office Application版本 /// </summary> /// <param name="name"></param> /// <returns>11/12/14/15/16</returns> /// <remarks>2003/2007/2010/2013/2016</remarks> private static int GetMSOfficeApplicationVersion(MSOfficeApplication name) { string TempString = string.Empty; Microsoft.Win32.RegistryKey RegKey = null; Microsoft.Win32.RegistryKey RegSubKey = null; // 組出registry key 名稱 TempString = GetMSOfficeApplicationName(name); if (string.IsNullOrEmpty(TempString)) { return(int.MinValue); } TempString = string.Format(@"{0}.Application\CurVer", TempString); // OpenBaseKey RegKey = OpenBaseKey(RegistryHive.ClassesRoot); RegSubKey = RegKey.OpenSubKey(TempString, false); if (RegSubKey == null) { return(int.MinValue); } object TempData = null; // Get default value TempData = RegSubKey.GetValue(null); if (TempData == null) { return(int.MinValue); } // 取得末2碼版本代表號 TempString = TempData.ToString(); TempString = TempString.Substring(TempString.Length - 2); int n; if (int.TryParse(TempString, out n) == false) { return(int.MinValue); } return(int.Parse(TempString)); }