vb.net 应用程序设置保存在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17244665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where are application settings saved?
提问by George
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
我正在尝试存储一些持久的应用程序数据,因此我通过 R 单击它,选择属性,然后选择“设置”选项卡并手动输入设置名称、类型、范围和值,向项目添加了一个设置
When I run the code, I read settings like this:
当我运行代码时,我会读取如下设置:
lastRunTime = My.Settings.LastRunTime
lastRunTime = My.Settings.LastRunTime
and set it like this:
并像这样设置:
My.Settings.LastRunTime = lastRunTime
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?
我在哪里可以真正看到新设置?因为看在上帝的份上,我看不到它在哪里更新设置。app.config 在我创建该设置时具有原始设置值。那我在哪里可以看到呢?
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?
当我运行代码时,我看到它更新了它,下次运行应用程序时,新值仍然存在,所以我知道它将它存储在某个地方。但是哪里?
回答by Hans Passant
.NET has to do something special, it has to give you a guarantee that anotherprogram that also happens to have a "LastRuntime" setting doesn'toverwrite 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
.NET 必须做一些特别的事情,它必须向您保证另一个程序也恰好具有“LastRuntime”设置不会覆盖为您的程序存储的值。为此,它将 user.config 文件存储在一个很难找回的目录中。它有一个奇怪的名字,比如
C:\Users\username\AppData\Local\WindowsFormsApplication1\WindowsFormsApplication1._Url_twchbbo4atpsvjpauzkgkvesu5bh2aul\1.0.0.0\user.config
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.
请注意项目名称如何成为路径的一部分,这是找到它的一种方法。名称中不可描述的部分是一个hash,它是由项目的各种属性创建的,这些属性使您的应用程序足够独特,不会与另一个 .NET 程序发生冲突,即使名称匹配。像您的产品名称、公司名称、exe 名称等。
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.
请注意反过来也是如此,更改此类属性会使您丢失 user.config 文件。因此,如果“LastRuntime”是某种许可证计量值,那么使用设置并不是最好的主意。
回答by Stephen Wrighton
That value is stored in the APPDATA folder under your user profile. For Windows 7 look in C:\Users\\AppData\Roaming\
该值存储在您的用户配置文件下的 APPDATA 文件夹中。对于 Windows 7,请查看 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.
将有一个基于你配置 Visual Studio 的方式命名的文件夹。由于我上班时的副本是注册到公司的,所以默认值是我的公司名称。由于我家里的Visual Studio是自己注册的,所以默认值是我的名字。
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.
AppData 是一个隐藏文件夹,如果您通过 Windows 资源管理器导航,它将不会显示。但是,如果您将它输入到运行命令中,它将毫无问题地打开。
回答by Joel Coehoorn
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.
对于机器级设置,项目中的 app.config 文件仅存储用于开发的原始设置,因此当您完成构建应用程序并将其部署到生产或交付给您的客户时,该默认值将首先出现安装。
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.
当您在调试期间更改该值时,您的程序(默认情况下)位于/bin/Debug/项目根目录之外的文件夹中。那里将有另一个 app.config 文件,其中包含项目作为文件名的一部分:project.app.config. 这是您保存的设置的位置。
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.
这里要记住的一件重要事情是,默认情况下,您的应用程序部署到 Program Files 目录中的文件夹中,而且标准用户默认情况下没有对该位置的写访问权限。这意味着标准用户无法更改应用程序级别的设置。
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.
对于用户级设置,也会发生类似的过程。但是,有一个重要的区别。它不是与您的应用程序位于同一文件夹中,而是使用您用户的 AppData 文件夹。这意味着标准用户确实具有写入权限,并且可以更改设置。但是,这意味着找到您的配置文件有点棘手,因为您必须防止两个具有相同名称的应用程序试图将配置文件存储在同一文件夹中。
回答by matzone
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

