Where are application settings saved?

2020-02-01 15:46发布

问题:

I am trying to store some persistent application data so I've added a setting to the project by R-Clicking on it, selecting properties, then Settings tab and manually entering settings name, Type, Scope and Value

When I run the code, I read settings like this:

lastRunTime = My.Settings.LastRunTime

and set it like this:

My.Settings.LastRunTime = lastRunTime

Where can I actually see the new setting? Because for the love of God, I cannot see where it is updating the setting. app.config has the original setting value when I created that setting. So where can I see it?

When I run the code, I see it updates it and the next time I run the app the new value persists, so I know it stores it somewhere. But where?

回答1:

.NET has to do something special, it has to give you a guarantee that another program that also happens to have a "LastRuntime" setting doesn't overwrite the value that's stored for your program. To do that, it stores a user.config file in a directory that's hard to find back. It has a weirdo name, like

C:\Users\username\AppData\Local\WindowsFormsApplication1\WindowsFormsApplication1._Url_twchbbo4atpsvjpauzkgkvesu5bh2aul\1.0.0.0\user.config

Note how the project name is part of the path, that's one way to find it back. The unspeakable part of the name is a hash, created from various properties of your project that make your app unique enough to not collide with another .NET program, even if the name matches. Like your product name, company name, exe name, etcetera.

Note how the converse is true as well, changing such a property makes you lose your user.config file. So if "LastRuntime" is some kind of license metering value then using a setting isn't the best idea.



回答2:

That value is stored in the APPDATA folder under your user profile. For Windows 7 look in C:\Users\\AppData\Roaming\

There will be a folder named based on how you configured Visual Studio. Since my copy at work is registered to the company, the default value is my company name. Since my copy of Visual Studio at home is registered to myself, the default value is my name.

AppData is a hidden folder, that won't show up if you navigate via Windows Explorer. But if you type it into the run command it will open with no problems.



回答3:

It depends on if this is a machine-level setting or a user level setting.

For machine-level settings, the app.config file in your project only stores the original setting used for development, so that when you finish building the app and deploy it to production or deliver to your customer, that default value will be there on first install.

When you change the value during debugging, your program is (by default) in a /bin/Debug/ folder off the root of the project. There will be another an app.config file there that includes the project as part of the file name: project.app.config. This is where your saved settings go.

An important thing to remember here is that, by default, your app is deployed to a folder in the Program Files directory, and also that standard users, by default, don't have write access to this location. This means that application level settings can't be changed by standard users.

For user-level settings, a similar process happens. However, there is one important difference. Instead of living in the same folder as your app, it uses your user's AppData folder instead. This means that standard users do have write access, and the ability to change the setting. However, it means finding your config file is a bit tricky, because you have to protect against two applications with the same name trying to store the config file in the same folder.



回答4:

Since you did

My.Settings.LastRunTime = lastRunTime
My.Settings.Save() '-----------------> maybe you forgot this

I'm sure you can read the setting with ..

lastRunTime = My.Settings.LastRunTime