Solved

Error reading the app.config file from a Module

Posted on 2011-09-30
13
577 Views
Last Modified: 2012-06-22
I was trying to read the app.config file using the code attached but I got the intellisense saying that that is an obsolete method.

Imports System.Configuration

Module modIntro
    Dim value1 As String = ConfigurationSettings.AppSettings("key1")
    Dim value2 As String = ConfigurationSettings.AppSettings("key2")
End Module

Open in new window

0
Comment
Question by:dimensionav
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36895316
Try My.Settings.key1 and My.Settings.key2.

Visual studio generates a My.Settings class that is compiled into your application and has properties for all the settings portion of app.config.
0
 
LVL 5

Expert Comment

by:AlokJain0412
ID: 36896232
This is the Good Blog to Understand
Please Go through
If you are with .net 2.0
http://geekswithblogs.net/akraus1/articles/64871.aspx

If you are with .net 4.0
http://geekswithblogs.net/akraus1/Default.aspx
0
 
LVL 5

Expert Comment

by:AlokJain0412
ID: 36896276
try its too

To be able to use the ConfigurationManager class you need to Add a Reference to System.Configuration in your Project References.
So to add a Reference:
1.       Right Click on References in your project in Visual Studio
2.       Click on Add Reference
3.       Click on the .NET Tab
4.       Select System.Configuration
5.       Click OK
Now you are all set to use the ConfigurationManager Class.
0
 

Author Comment

by:dimensionav
ID: 36897872
Guys I have created the reference that you mentioned and I am getting the app settings like this:

Dim DBPath as String = My.Settings("DBPath")
But I got this inner exception error: {"The settings property 'DBPath' was not found."}

My app.confg file is like the attached code.

 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
                 initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>
  <appSettings>
    <add key="DBPath" value="Z:\temp\emp1\" />
  </appSettings>
</configuration>

Open in new window

0
 

Author Comment

by:dimensionav
ID: 36897949
I have read that app settings must be added / removed from the project designer (which is empty), I am confused about creating the app.config manually and adding settings to it directly.

Thanks in advance.
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36897964
Looks as if you wrote the appsettings section yourself. You are working to hard :-). Let the system work for you.

First of all, it is not in the right format.

Second, in order for My.Settings to work, Visual Studio must create the My.Settings class that provides a property for each setting. It won't do it is you type in app.config. In fact, there are very few operations that requires you to write in the configuration file. Visual Studio will do it for you most of the time.

Delete your <appSettings> section.

Go to the project properties window (Project menu or Right-click on the project in the Solution Explorer).

There is a Settings tab. Enter your setting(s) with the type(s) and default value(s). For each setting, select User or Application.

An application setting is readonly and cannot be changed by the application. An administrator will have to change it directly in the .config file if he wants to make changes once the application is distributed, and all users will get the same setting.

A User setting can be changed by the user, and will also be different for each user once it has been changed. When a user starts the application for the first time, all the users settings are copied, with their defaults, to a file in the user's personal folders. When changes are made, they are made there. So each user can have a different set of User settings.

This will enter the proper entry in app.config, and will in parallel generate the class that is used when you call My.Settings.

Dim path As String
path=My.Settings.DBPath                 'Reads the setting into the application
My.Settings.DBPath = "C:\YourPath" 'Saves the setting in memory
My.Settings.Save                             'Commit all the changed settings to the personal user's .config file



 
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:dimensionav
ID: 36897981
JamesBurger...now is pretty clear!

So, I should delete and create a new app.config file or even that (create it)  the system can do ?
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36898003
Do not create a new file, because you already have something else there. The config file is use for more than the settings, and it looks as if you are using a TraceSwitch in your application.

Delete only the section that has to do with the settings:

  <appSettings>
    <add key="DBPath" value="Z:\temp\emp1\" />
  </appSettings>

When you start working in the Settings tab, it will take care of everything for you, and My.Settings will work.
0
 

Author Comment

by:dimensionav
ID: 36898041
A final question:
I was planning to store sql code in some of this keys, do you think that a query is too much text for this purpose?
0
 
LVL 40
ID: 36898305
What database are you using?

The best place to store SQL is in the database itself. It is called a query in Access, a view or a stored procedure (depending on its complexity) in SQL Server.

The configuration file is definitively not a place to store SQL. It could store the connection string, user's preferences, the status of the application when it was last close, but not code. And SQL is code.
0
 

Author Comment

by:dimensionav
ID: 36901983
I am not using any database but a recommendation would be great, consider that the application is very small, it performs a query, creates an XML file and then uploads it to a website, this runs at most every hour.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36902441
>I am not using any database
But you want to run a query? Strange. If you are using SQL Server, you can create a stored procedure and then call it from your code.

To answer your original question, replacing ConfigurationSettings with ConfigurationManager (as suggested previously I think) should have solved your problem.

My.Settings is easy but the My namespace is not available in C# whereas ConfigurationManager can be used in both VB and C#.
0
 

Author Comment

by:dimensionav
ID: 36903427
CodeCruiser:

I am not using a database for configuration settings but I use VFP (DBF) tables from where I extract information.
0

Featured Post

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).

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

20 Experts available now in Live!

Get 1:1 Help Now