写注册表添加Office授权目录

简介: public class RegisOffice { public static IList GetOfficeRegistryKeyInfo() { IL...
public class RegisOffice
    {
        public static IList<OfficeVersion> GetOfficeRegistryKeyInfo()
        {
            IList<OfficeVersion> registriedOfficeVersionList = new List<OfficeVersion>();
            RegistryKey rkCurrentUser = Registry.CurrentUser;
            foreach (var officever in Enum.GetValues(typeof(OfficeVersion)))
            {
                if (IsRegistrationOfficeByOfficeVersion((OfficeVersion)officever))
                    registriedOfficeVersionList.Add((OfficeVersion)officever);
            }
            return registriedOfficeVersionList;
        }

        private static bool IsRegistrationOfficeByOfficeVersion(OfficeVersion officeVersion)
        {
            RegistryKey rkCurrentUser = Registry.CurrentUser;
            RegistryKey rkRegistration = rkCurrentUser.OpenSubKey(string.Format(@"Software\Microsoft\Office\{0}.0\Registration\", (int)officeVersion));
            return rkRegistration == null ? false : true;
        }

        public static void AddOfficeReliancePath()
        {
            try
            {
                string filePath = Environment.CurrentDirectory + "\\" + "your directory" + "\\";
                AddOfficeReliancePath(filePath);
            }
            catch (Exception ex)
            {
               //添加信任地址失败
            }
        }

        public static void AddOfficeReliancePath(string path)
        {
            IList<OfficeVersion> registriedOfficeVersionList = GetOfficeRegistryKeyInfo();
            foreach (var ov in registriedOfficeVersionList)
            {
                AddOfficeReliancePath(ov, OfficeComponents.Word, path);
                AddOfficeReliancePath(ov, OfficeComponents.PowerPoint, path);
            }
        }

        public static void AddOfficeReliancePath(OfficeVersion officeVersion, OfficeComponents officeComponents, string path)
        {
            RegistryKey rkCurrentUser = Registry.CurrentUser;
            RegistryKey rkLocation = rkCurrentUser.OpenSubKey(string.Format("Software\\Microsoft\\Office\\{0}.0\\{1}\\Security\\Trusted Locations", (int)officeVersion, officeComponents.ToString()), true);
            RegistryKey rkLocationSmartLesson = rkLocation.CreateSubKey("LocationSmartLesson");
            rkLocationSmartLesson.SetValue("Path", path);
        }
    }

    public enum OfficeVersion
    {
        Office2003 = 11,
        Office2007 = 12,
        Office2010 = 14,
        Office2013 = 15,
        Office2016 = 16
    }

    public enum OfficeComponents
    {
        Word = 1,
        PowerPoint =2,
        Excel =3,
        Access =4,
        Outlook =5 
    }

目录
相关文章
|
Java 数据中心 Windows
Confluence 6 配置 Windows 服务
当你使用 Start Confluence Automatically on Windows as a Service 的方式启动的时候,你有下面 2 种方式来配置你的系统属性:通过 command line 或者 in ...
1423 0
|
数据安全/隐私保护 Windows