加密Web.config文件的连接字符串时,未处理ConfigurationErrorsException

时间:2015-09-01 14:01:43

标签: c# winforms encryption

我尝试通过简单的winforms应用程序加密web.config文件的连接字符串部分。我以管理员身份运行VS2010并运行以下代码部分。

EXEC

我尝试从物理路径位置打开文件,并将加密信息写回其中。但是config.Save()向我提出了以下错误。

 private void encryptButton_Click(object sender, EventArgs e)
        {
            Configuration config = GetConfiguration();
            ConfigurationSection configSection = config.GetSection("connectionStrings");
            if (configSection != null)
                // Only encrypt the section if it is not already protected
                if (!configSection.SectionInformation.IsProtected)
                {
                    configSection.SectionInformation.ProtectSection(
                        "DataProtectionConfigurationProvider");
                    configSection.SectionInformation.ForceSave = true;
                    config.Save();
                    DisplayWebConfig();
                }
        }

private Configuration GetConfiguration() 
        {
            var configFile = new FileInfo(@"C:\Users\abalawan\Desktop\CN\R13 new\Websites\ABC");
            var vdm = new VirtualDirectoryMapping(configFile.DirectoryName,true,configFile.Name);
            var wcfm = new WebConfigurationFileMap();
            wcfm.VirtualDirectories.Add("/",vdm);
            return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
        }

Stacktrace如下所示:

ConfigurationErrorsException was unhandled.

Unable to save config to file 'C:\Users\abalawan\Desktop\CN\R13 new\Websites\ABC'.

0 个答案:

没有答案
相关问题