Solved

AppSetting in C# in WinForm App

Posted on 2009-07-12
8
1,717 Views
Last Modified: 2013-12-17
I'm doing something stupid, but can't see it.

Trying to read a configuration value out of app settings. File is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
      <configSections>
  <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
   <section name="CSharpPushKDX.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </sectionGroup>
 </configSections>
 <applicationSettings>
  <CSharpPushKDX.Properties.Settings>
   <setting name="LogFilePath" serializeAs="String">
    <value>C:\Saros\SarosNet35\PushKDX\PushKDX\PushKDX%d%.log</value>
   </setting>
  </CSharpPushKDX.Properties.Settings>
 </applicationSettings>
</configuration>

Code is:

            string lsAnswer = "";
            try
            {
                lsAnswer = System.Configuration.ConfigurationManager.AppSettings.Get("LogFilePath");
                lsAnswer = System.Configuration.ConfigurationManager.AppSettings["LogFilePath"];
                lsAnswer = ConfigurationSettings.AppSettings["LogFilePath"];
            }
            catch
            {
                lsAnswer = "";
            }

Note that I have tried to get the desired value three different ways. All return null.

I'm trying to switch over from VB.NET to c# with this project, so I expect some dumb translation issue.

Thanks...
0
Comment
Question by:kwebster7327
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:eirikurh
ID: 24835774
try
string lsAnswer = Properties.Settings.Default.LogFilePath
0
 
LVL 6

Expert Comment

by:microbolt
ID: 24835780
Try it like this:

lsAnswer = Properties.Settings.Default.LogFilePath
0
 
LVL 1

Author Comment

by:kwebster7327
ID: 24835843
The name 'Properties' does not exist in the current context.
0
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.

 
LVL 6

Accepted Solution

by:
microbolt earned 500 total points
ID: 24835875
Make sure there there are no namespace issues. The fully qualified name of the Resources class would be YourProject.Properties.Resources, so if something has caused the Form to be placed in a different namespace from your Properties class, you would need to use the fully qualified name to access it from the form. Use the Object Browser to browse your solution and figure out what the namespace of your Properties class is.
0
 
LVL 1

Author Comment

by:kwebster7327
ID: 24836607
Close! If I move it back into the main namespace, it. works.

I'm trying to make this part of a general-purpose, reusable function, so I guess I'll need to figure out how to pass the namespace name into it?

Also, this references the default value of the parameter. If the value gets changed after deployment, will that new value get picked up or will we always get the default?

Thanks...
0
 
LVL 6

Expert Comment

by:microbolt
ID: 24836944
You can just Import the main namespace at the top of your file and it should work as well.  Then when you use it in multiple projects just change the Import at the top.
0
 
LVL 1

Author Comment

by:kwebster7327
ID: 24839565
Assume you me "using" instead of "imports" - this is C#.

0
 
LVL 6

Expert Comment

by:microbolt
ID: 24844601
Yep, had an brain fart =D
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting error in connectionstring with Excel. 30 45
JSON  parse help 7 40
SSRS Deployment problem 5 63
VS 2013 installer,  Install, Update and uninstall via SCCM 6 25
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

789 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