Solved

accessing application settings from a .NET console app

Posted on 2008-10-08
7
2,014 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 18

Expert Comment

by:philipjonathan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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