Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have many VB winforms where I successfully use this. But today, nothing works. When I use this -- ConfigurationSettings.AppSettings() -- VS shows it has been replaced with something else. And that does not work either. I'm using VS 2017.

What I have tried:

I've tried what has worked in the past and the VS intellisense advice. None work.
Posted
Updated 31-Dec-22 11:11am

1 solution

I am assuming that you are using .Net Framework and not DotNet Core.

If I remember correctly, you should do something like:
VB
Dim ConnectionString = ConfigurationManager.ConnectionStrings["StringName"].ConnectionString

Where StringName is the key in your settings file.

Update

Here is the official documentation: ConfigurationManager Class (System.Configuration) | Microsoft Learn[^]

Update #2

I found some time to fire up VS on New Year's Day...

App Type: Console
Target Framework: .Net Framework 4.7.2 (also works with 4.5)

Nuget Package: NuGet Gallery | System.Configuration.ConfigurationManager 7.0.0[^]

1. App.config
XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
    <connectionStrings>
        <add name="default" connectionString="This is my connection string"/>
    </connectionStrings>
</configuration>

2. Program.vb
VB.NET
Imports System.Configuration

Module Module1

    Sub Main()

        Dim ConnectionString = ConfigurationManager.ConnectionStrings("default").ConnectionString
        Console.WriteLine($"[{ConnectionString}]")

    End Sub

End Module

Output:
[This is my connection string]

Enjoy!
 
Share this answer
 
v4
Comments
BobbyStrain 31-Dec-22 19:35pm    
Thanks, I went to the official document and used exactly the code presented for VB .NET 4.5. It did not work! ConfigurationManager is not recognized.
Graeme_Grant 31-Dec-22 19:50pm    
Did you use:
Imports System.Configuration
BobbyStrain 31-Dec-22 21:33pm    
Yes, thank you. The behavior is strange. I'll try it next week on my laptop with VS 2019.
Graeme_Grant 31-Dec-22 21:35pm    
Works fine using the test above. Create a new project, just like I did, and use my code and config above.
BobbyStrain 31-Dec-22 21:41pm    
Thanks. I'm trying to keep my pup calm. He knows the bangs! are soon coming.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900