Link to home
Start Free TrialLog in
Avatar of dimensionav
dimensionavFlag for Mexico

asked on

Error reading the app.config file from a Module

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

ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
Avatar of dimensionav

ASKER

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

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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.
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.
Avatar of Nasir Razzaq
>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#.
CodeCruiser:

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