Solved

Why does saving application settings in a C# application generate App.config.config?

Posted on 2014-12-18
6
343 Views
Last Modified: 2014-12-22
I'm currently working out how to use the .net application settings infrastructure in C#. I've implemented a code snippet I received from: http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx

The code is as follows:
public void ToggleConfigEncryption(string exeConfigName)
        {
            // Takes the executable file name without the 
            // .config extension. 
            try
            {
                // Open the configuration file and retrieve  
                // the connectionStrings section.
                Configuration config = ConfigurationManager.
                    OpenExeConfiguration(exeConfigName);

                ConnectionStringsSection section =
                    config.GetSection("connectionStrings")
                    as ConnectionStringsSection;

                if (section.SectionInformation.IsProtected)
                {
                    // Remove encryption.
                    section.SectionInformation.UnprotectSection();
                }
                else
                {
                    // Encrypt the section.
                    section.SectionInformation.ProtectSection(
                        "DataProtectionConfigurationProvider");
                }
                // Save the current configuration.
                config.Save();

                Console.WriteLine("Protected={0}",
                    section.SectionInformation.IsProtected);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

Open in new window


The App.config looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <connectionStrings>
    <clear />
    <add name="SQL"
         providerName ="System.Data.SqlClient"
         connectionString ="Server=MyServer; Database=MyDB; User Id=sa; Password=Password;"/>
  </connectionStrings>
</configuration>

Open in new window


What I don't understand is why issuing the config.save(); command saves the application settings into the App.config.config file instead of the App.config file its self. Does anyone know why?
0
Comment
Question by:DonovanV
6 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40508462
Are you sure you are passing in the exe name without the .config extension?

Place a break point on the code and check the value exeConfigName at runtime
0
 

Author Comment

by:DonovanV
ID: 40509670
Oops, I forgot to list my input. I've tested passing both "MyAppName.exe" and "App.config" into the ToggleConfigEncryption procedure. The result I described was from passing "App.config". When I pass "MyAppName.exe" into that procedure the result is a file named "MyAppName.exe.config". In both cases the connection string I'm trying to protect is left unencrypted in the App.config file.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40510464
This is normal. App.config is part of your source code. It is not distributed with your application, so it does not need to be encrypted.

The one that you distribute with your application is the MyAppName.exe.config, so this is the one that needs to be encrypted. That is why you need to pass MyAppName.exe as a parameter, so that the MyAppName.exe.config file is the one that is encrypted.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 40510533
I strongly agree with Jacques Bourgeois (James Burger).
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40511057
During testing the file being edited it located in the bin/Debug folder and as noted will be named MyAppName.exe.config
When you distribute the app you will need to ensure it is not encrypted as the encryption is machine specific and another machine will not have the appropriate keys needed to decrypt the string.
http://weblogs.asp.net/jongalloway/encrypting-passwords-in-a-net-app-config-file
0
 

Author Closing Comment

by:DonovanV
ID: 40513428
Jacques cleared up the exact confusion I had about how this system was supposed to work. I think it is important however to point out that Michael74's answer is very important to the concept of encrypting and deploying settings. Now that I know how the system works I know that the way I was trying to implement it would not have been workable in the end without having un-encrypted settings deployed with the app then cleaned up later, at the very least.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question