CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2007
    Posts
    56

    Regarding .config files

    Hï!
    If the "AddrColWidths" entry is missing in the config file my last code line throws an exception. Is there a common way to check if the entry exists before trying to read it?

    I also wonder if there is an easy way to check wether the .config file exists, for example through the config object. I cant find such a way.

    Code:
    String ^exePath = Reflection::Assembly::GetExecutingAssembly()->Location;
    System::Configuration::Configuration ^config =  
                                    ConfigurationManager::OpenExeConfiguration(exePath);
    AppSettingsSection ^appSettingsSection =
                                    (AppSettingsSection^)config->GetSection("appSettings");
    CommaDelimitedStringCollectionConverter^ converter = 
                                    gcnew CommaDelimitedStringCollectionConverter();
    CommaDelimitedStringCollection^ collection =
    (CommaDelimitedStringCollection^)converter->ConvertFrom(appSettingsSection->Settings["AddrColWidths"]->Value);
    Best regards
    Mattias
    .NET 3.5, VS 2008 Express, SQL Server 2008 Express

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Regarding .config files

    from my experience I'd discourage you from using .config files. I have always had problems with lost settings in a new app version etc. now I use the windows registry and all problems are gone. (I was to lazy to write my own config wrapper so using the registry was easier but the default .net .config files are evil)
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Feb 2007
    Posts
    56

    Re: Regarding .config files

    Well, maybe you´re right, but still, is there an answer to my questions?
    .NET 3.5, VS 2008 Express, SQL Server 2008 Express

  4. #4
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Regarding .config files

    I think the best way is to catch the ConfigurationErrorsException like in msdn examples. some config wrapper could be very useful.
    Last edited by memeloo; January 16th, 2010 at 12:29 PM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  5. #5
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Regarding .config files

    You can check for nulls :

    Code:
    System::Configuration::Configuration ^config =  
        ConfigurationManager::OpenExeConfiguration(exePath);
                                    
    if (config != nullptr)
    {                                
        AppSettingsSection ^appSettingsSection =
            (AppSettingsSection^)config->GetSection("appSettings");
            
        if (appSettingsSection != nullptr)
        {
            if (appSettingsSection->Settings["AddrColWidths"] != nullptr)
            {        
                CommaDelimitedStringCollectionConverter^ converter = 
                    gcnew CommaDelimitedStringCollectionConverter();
            
                CommaDelimitedStringCollection^ collection =
                    (CommaDelimitedStringCollection^)converter->ConvertFrom(appSettingsSection->Settings["AddrColWidths"]->Value);
            }
        }
    }
    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured