Solved

accessing application settings from a .NET console app

Posted on 2008-10-08
7
2,019 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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