Solved

accessing application settings from a .NET console app

Posted on 2008-10-08
7
2,021 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ModalPopup  question 22 56
Web page design problem 3 41
PowerShell: ForEach-Object Export to CSV 4 92
CSS styling problem 3 27
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…

739 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