Solved

accessing application settings from a .NET console app

Posted on 2008-10-08
7
2,018 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
  • 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
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.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now