写注册表添加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 
    }

目录
相关文章
|
Windows
Windows中静默导入注册表
Windows中静默导入注册表
189 0
|
Windows
WINDOWS如何编写注册表文件
WINDOWS如何编写注册表文件
118 0
|
存储 监控 安全
C# 监控 Windows 文件夹
C# 监控 Windows 文件夹
241 0
C# 监控 Windows 文件夹