Solved

accessing application settings from a .NET console app

Posted on 2008-10-08
7
2,020 Views
Last Modified: 2013-12-17
I am looking for a way to read a config file for a .NET console app.  I keep finding info for ASP .NET apps.
My goal is to have an app.config file associated with the .exe so that  once deployed  the system admin can set app flags by merely editing the xml file.
System admin types usually dont want to fire up Visual Studio to change app settings.

Additionally, I cannot even seem to access the settings in the settings.settings file  the My.Settings object doesnt seem to exist for C# programs.

Thanks for any help or pointers you can give me.
Ken
0
Comment
Question by:kenfhenderson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 18

Assisted Solution

by:philipjonathan
philipjonathan earned 500 total points
ID: 22674890
The basic concept is the same. You can add a file "app.config" to your project, alternatively you can right click on your project, choose "Add Item", then choose the item type "Configuration File".

Once added, you can add configuration settings the same way you would to ASP.NET config file. Something like:

<appSettings>
  <add key="ConfigKey" value="1234" />
</appSettings>

Then from your code:

string configStr = ConfigurationSettings.AppSettings["ConfigKey"];

Need to add:
using System.Configurations;
0
 

Author Comment

by:kenfhenderson
ID: 22675013
I tried your code exactly and it returned a null string.
Interestingly, there was already an app.config as part of the project. It had my values in it and they matched the values in the settings.settings file that was also part of the project.
When I look in the bin/debug directory with the .dll file, I do see the appname.dll.config file which does have the right XML in it. However, I cannot seem to access it from the code.
I have used both the ConfigurationSettings class and the ConfigurationManager classes.
some code snippets are posted - the string variables come up null.
I do have a "using System.Configuration;"
Thanks for your help -Ken

            string configStr = ConfigurationSettings.AppSettings["ActualsInputFolder"];
 
            string[] names = ConfigurationManager.AppSettings.AllKeys;
            NameValueCollection appStgs = ConfigurationManager.AppSettings;
 
            for (int i = 0; i < appStgs.Count; i++)
            {
                Console.WriteLine("#{0} Name: {1} Value: {2}",
                  i, names[i], appStgs[i]);
            }
 
            // GET FILESPEC FROM .CONFIG FILE (in same try block)
            try
            {
 
                string ACTUALSINPUTFOLDER = ConfigurationManager.AppSettings["ActualsInputFolder"];
            }

Open in new window

0
 

Accepted Solution

by:
kenfhenderson earned 0 total points
ID: 22675040
In my web surfing, I found the following - that has worked!
string test = Properties.Settings.Default.ActualInputFolder;
actually sets the test string to the path in my settings.settings (or app.config) file.
The "ActualInputFolder" is my keyname.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 18

Expert Comment

by:philipjonathan
ID: 22675061
If you add the config file to the DLL project, I don't think the application will read it. Try adding the config file to the application instead.
0
 

Author Comment

by:kenfhenderson
ID: 22675191
By getting the value of the property "Properties.Settings.Default.mykeyname", I was able to use the files that were created by VS2005 when I created some settings in Project Designer. The app.config was named "myappname.dll.config" in the same folder as the .dll.  I was able to read the settings value using the above property.
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 22675251
Alright, I posted my last comment before reading your comment ID:22675040. But glad that you manage to solve it. I'm learning something new too.
0
 
LVL 5

Expert Comment

by:harwantgrewal
ID: 22675539
HI philipjonathan
Basically you have to add a new file called Settings file and name it whatever you want its just a which has your values. Lets we call this file as Settings1.settings and I have created a string value ConfigKey you can straightly access the value like



using System;
using System.Collections.Generic;
using System.Text;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string yourvariable = Settings1.Default.ConfigKey
        }
    }
}

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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