Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

Application Settings in VB.NET 2008

"app.config" and Applications

By Dan Mabbutt, About.com

Jan 27 2008

After changing the background color in our sample application, let's look at app.config. (You can examine it using VB.NET by double clicking it, or in some other tool such as Notepad or IE.)

<userSettings>
   <TestUserConfig.My.MySettings>
      <setting name="myBackColor" serializeAs="String">
         <value>Yellow</value>
      </setting>
      <setting name="Billionaire" serializeAs="String">
         <value>"Bill"</value>
      </setting>
   </TestUserConfig.My.MySettings>
</userSettings>

The actual "User" settings used aren't saved here, however. For a single user computer, they're stored in the location in C:\Users\<user id>\AppData\Local\ in a location with a cryptographically generated name the first time they're saved in my experimentation. (I was also not able to get the Synchronize button to do what Microsoft's documentation said it should do: delete User.config files. I was able to find a number of unanswered questions from people with the same problem. I think this is a bug in VB.NET. If anyone has any wisdom to share on this point, I'd love to hear it.)

So, if you have a value that you need to save between executions, say, the name of the person who used your program last, you can save it here (Sub parameters omitted to shorten lines):

Public Class Form1
   Private Sub DispLastUser_Click(ByVal ...
      LastUserDisplay.Text = My.Settings.LastUserID
   End Sub
   Private Sub LoginUser_Click(ByVal ...
      My.Settings.LastUserID = LoginID.Text
   End Sub
End Class

In our example so far, the scope of the setting has been set to User. If you code the same application with a scope of Application, you discover that the statement ...

   My.Settings.myBackColor = ColorDialog1.Color

... now generates an error: "Property 'myBackColor' is ReadOnly." If you think about it, this only makes sense for an application scoped setting. It would be chaos if individual applications could change application settings. What if one application changed a database connection string while another application was connecting and disconnecting to the database? With User scoped settings, the value persisted is the one in the instance of the application that was closed last. To change these, you need to use the Settings tab in Project Designer again. (Select Properties under the Project menu or right-click the project in Solution Explorer and select Properties from the context menu.)

Do you need to persist the high score for the hot new game you're coding? Maybe the previous order for an online catalog? This could be exactly what you're looking for.

Explore Visual Basic

By Category

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Application Settings in VB.NET 2008

©2009 About.com, a part of The New York Times Company.

All rights reserved.