简体   繁体   中英

How does AppSettings in VB.NET windows Forms work?

Hello I have build a small app to demo a concept in C# in which I added the application config file etc added System.Configuration dll to the reference and accessed the settings:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
   <add key="username" value="myknownusername"/>
 </appSettings>
</configuration>

No news It worked flawlessly with the code:

private String username = ConfigurationManager.AppSettings["username"];

Now I have been asked to do the port the app to VB.NET and to my biggest surprise. It's been a while that I opened visual studio in VB.NET project. I was surprised to see an already white dashed App.config file so I added my AppSettings section and after 3 hours I still can't get the value of the username using the same ConfigurationManager

Dim username as String = ConfigurationManager.AppSettings("username")

I have included another application configuration app1.config which by the way has generated entries already. I could not get anything with the app1.config either.It also returns Nothing

       </sharedListeners>
    </system.diagnostics>
</configuration>

I am really perplexed as how a simple reading of configuration file could be this challenging in VB.NET unless I am using the wrong method which I always use in C#.

Kindly point me to whatever I am not doing right.

EDIT

As you can see in the picture below, I have all it needs to work properly.I as expecting to read the setting key from either App.config or app1.config. When I run like shown below the MessageBox is empty

该项目的屏幕截图

I found the culprit, I needed to right click the generated App.config and choose Include In Project . that's it. it's weird because I never do that in a C# project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM