none
System.Configuration.ConfigurationManager.AppSettings["miClave"] returns NULL RRS feed

  • Question

  • Hi,

      I'm developing an Windows application , I started in VS2005 a now I have it on VS2010.

      When I call System.Configuration.ConfigurationManager.AppSettings["miClave"], it returns null if the config file doesn't exist (I create the file before I try to read it), but It works fine if the config file exists before running the application.

    The question is that in VS2005 I think it worked fine. If the file doesn't exist, I create it in Program.cs, before calling Applicatoin.Run(form1), even before calling the form constructor...

    What's happening?How can I enforce the environment to read the file I just created?

    My Programs.cs:

            static void Main(string[] args)
            {
                ...
                {
                    FormIEC870_5_102.WriteDefaultConfigFile();
                    ...
                    if (licenseIsValid)
                    {
                        Form form1 = new FormIEC870_5_102(autoModeParam, GetCountersLimitFromLicense(),theLicenseValidator);
    
                        Application.Run(form1);
                    }
                }

    Here is how I create the config file:

        public static void WriteDefaultConfigFile()
            {
                StringCollection Document;
                String filename;
    
                filename = Application.ExecutablePath + ".config";
    
                if (!File.Exists(filename))
                {
                    Document = new StringCollection();
                    Document.Add("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    Document.Add("<configuration>");
                    Document.Add("<appSettings>");
                    Document.Add("<add key=\"miClave\" value=\"miValor\" />");
                    Document.Add("</appSettings>");
                    Document.Add("</configuration>");
                    WriteDocument(filename, Document);
                }
            }

    Thanks in advance

    Wednesday, February 15, 2012 3:50 PM

Answers

  • Then it should work. And it is working in my machine.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by pedrojpzz Wednesday, February 15, 2012 11:03 PM
    Wednesday, February 15, 2012 5:48 PM
  • Ok, you are right, I have tested a new project with this simple code and it works, so while making this project I have noticed that System.Configuration dll belongs to .NET 4.0 version, so I have find out that in my original project, System.Confiiguration dll belongs to .NET 2.0 version, so I changed it to version 4.0 and now it works fine.

    Thanks :)

    • Marked as answer by pedrojpzz Wednesday, February 15, 2012 11:03 PM
    Wednesday, February 15, 2012 11:03 PM

All replies

  • There is nothing wrong in the code. What is happening is - the configuration file you are creating is <AppName>.exe.config. But, if you are running your application in Visual Studio in debug mode (By pressing F5), the configuration file name should be <Appname>.vshost.exe.config. This file will be automatically created for you if there is an exiting <appname>.exe.config file. But, first time when you run the application in visual studio (in debug mode), there was not such file and hence <appname>.vshost.exe.config file won't be created and hence you will get NULL.

    What you can do is either run your application directly by double clicking the executable. On the other hand, in visual studio itself, run the application by pressing CTRL+F5. In both these cases, your program will work fine.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, February 15, 2012 5:22 PM
  • I'm testing my application out of Visual Studio, so this is not the problem.

    Thanks anyway.

    Wednesday, February 15, 2012 5:46 PM
  • Then it should work. And it is working in my machine.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by pedrojpzz Wednesday, February 15, 2012 11:03 PM
    Wednesday, February 15, 2012 5:48 PM
  • Ok, you are right, I have tested a new project with this simple code and it works, so while making this project I have noticed that System.Configuration dll belongs to .NET 4.0 version, so I have find out that in my original project, System.Confiiguration dll belongs to .NET 2.0 version, so I changed it to version 4.0 and now it works fine.

    Thanks :)

    • Marked as answer by pedrojpzz Wednesday, February 15, 2012 11:03 PM
    Wednesday, February 15, 2012 11:03 PM