How to make config files in Bin folder into non human-readable formats?(c#)

339 views Asked by At

After I setup a project, there are some .config files in bin folder. I want to make them non human-readable formats. 1. I know I can use command such as asp net_ reg i i s -p e ..., but the project is a Win Form Project, and in my config file there is no parts like Web.config. My config file formats is like below:

<...>
  <add key="..." value="...." />
  <add key="..." value="..." />
</...>
  1. I also tried to encrypt the config files, however there are so many places that call the config files. It seems too complex to do so.

So, is there some easy way to make my config files in bin folder into non human-readable formats using C#?

1

There are 1 answers

0
XeroxDucati On

You have two options.. If you are interested in protecting some special parameter, e.g. a password, you can just encrypt it, and only have the encrypted value. If you really want to make the whole thing protected, you can use SectionInformation.ProtectSection.

MSDN Sample code for that:

static public void ProtectSection()
{

    // Get the current configuration file.
    System.Configuration.Configuration config =
            ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);


    // Get the section.
    UrlsSection section =
        (UrlsSection)config.GetSection("MyUrls");


    // Protect (encrypt)the section.
    section.SectionInformation.ProtectSection(
        "RsaProtectedConfigurationProvider");

    // Save the encrypted section.
    section.SectionInformation.ForceSave = true;

    config.Save(ConfigurationSaveMode.Full);

    // Display decrypted configuration  
    // section. Note, the system 
    // uses the Rsa provider to decrypt 
    // the section transparently. 
    string sectionXml =
        section.SectionInformation.GetRawXml();

    Console.WriteLine("Decrypted section:");
    Console.WriteLine(sectionXml);

}