Beispiel #1
0
        private static void AddTrustDirToRegister(string trustDir, RegistryView registryView)
        {
            List <string>   listSubKeys     = new List <string>();
            var             localKey        = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, registryView);
            RegisterManager registerManager = new RegisterManager()
            {
                BaseKey = localKey
            };

            registerManager.EnumerateKeyNames(@"Software\Microsoft\Office", ref listSubKeys);
            CreateTargetSubKey(registerManager, listSubKeys, localKey, trustDir);
        }
Beispiel #2
0
        private static void CreateTargetSubKey(RegisterManager registerManager, List <string> listSubKeys, RegistryKey localKey, string trustDir)
        {
            var regAllowNetworkLocations = listSubKeys.Where(s => s.EndsWith(@"Excel\Security\Trusted Locations"));

            //设置信任网络路径
            foreach (var item in regAllowNetworkLocations)
            {
                registerManager.SetRegeditKeyValue(item, "AllowNetworkLocations", "1");
            }


            //包含EXCEL字样的,并且有location节点
            var listSecurity = listSubKeys.Where(s => s.Contains(@"Excel\Security\Trusted Locations")).Where(s => Regex.IsMatch(s, @"Location\d+$")).ToList();

            foreach (var item in listSecurity)
            {
                if (registerManager.IsRegeditKeyAndValueExist(item, "Path", trustDir))
                {
                    return;
                }
            }
            ;

            var result = from s in listSecurity
                         select new { GroupName = Regex.Match(s, @".+?\\.+?\\.+?\\.+?\\").Value, Fullpath = s };

            //按HKEY_CURRENT_USER\Software\Microsoft\Office\15.0分组,防止多个EXCEL版本的原因引起信任位置添加不全
            var query = from s in result
                        group s by s.GroupName;

            foreach (var item in query)
            {
                //只取第1条记录,去掉最后一个尾数
                string locationName = Regex.Match(item.First().Fullpath, @".+Location").Value;
                //用最后的尾数来比较大小,不是用字符串,可以最终比较出11比2大
                int         locationIndex   = item.Max(s => int.Parse(Regex.Match(s.Fullpath, @".+Location(\d+)").Groups[1].Value) + 1);
                string      newLocationName = Regex.Match(locationName, ".+Location").Value + locationIndex;
                RegistryKey Location        = localKey.CreateSubKey(newLocationName);
                Location.SetValue("Path", trustDir);
                Location.SetValue("AllowSubfolders", "00000001", RegistryValueKind.DWord);
                Location.SetValue("Date", DateTime.Now.ToString());
                Location.SetValue("Description", "");
            }
        }