Open web.config from console application?

后端 未结 4 1829
萌比男神i
萌比男神i 2020-12-13 05:09

I have a console capplication that runs on the same computer that hosts a bunch of web.config files. I need the console application to open each web.config file and decrypt

4条回答
  •  星月不相逢
    2020-12-13 05:54

        public static string WebKey(string key)
        {
    
            var configFile = new System.IO.FileInfo(webconfigPath);
            var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
            var wcfm = new WebConfigurationFileMap();
            wcfm.VirtualDirectories.Add("/", vdm);
            System.Configuration.Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
            System.Configuration.AppSettingsSection appSettingSection = (System.Configuration.AppSettingsSection)config.GetSection("appSettings");
            System.Configuration.KeyValueConfigurationElement kv = appSettingSection.Settings.AllKeys
                             .Where(x => x.Equals(key))
                             .Select(x => appSettingSection.Settings[key])
                             .FirstOrDefault();
    
            return kv != null ? kv.Value : string.Empty;
    
        }
    

提交回复
热议问题