.net Visual Studio 设置文件 - 它是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1001487/
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
Visual Studio Settings file - how does it work?
提问by pkolodziej
I do not understand the following things:
我不明白以下几点:
- What is the difference between app.config (applicationname.exe.config) and settings file ?
- I am unable to locate the *.settings file in Windows7 AppData directory under the specific account (I heard that it should be located somewhere over there) ?
- Let's assume that *.settings file is somewhere on the hard drive. Why on the development time the data from settings file are copied to application config file?
- app.config (applicationname.exe.config) 和设置文件有什么区别?
- 我无法在特定帐户下的 Windows7 AppData 目录中找到 *.settings 文件(听说它应该位于那里的某个地方)?
- 让我们假设 *.settings 文件在硬盘上的某个地方。为什么在开发时将设置文件中的数据复制到应用程序配置文件中?
Kind Regards PK
亲切的问候 PK
回答by blak3r
So the default settingsare stored in the <exename>.configfile located in the same directory as the executable.
因此,默认设置存储在<exename>.config与可执行文件位于同一目录的文件中。
The reason you do cannot find the user.config file is it is only created if you have made changes to the defaults. The user settings override the defaults which are stored in the <exename>.configfile. If no changes have been made, then no user configuration file will exist.And... once it does exist you will notice that only the settings which have been changes will show up in the user.config file.
您找不到 user.config 文件的原因是它只有在您对默认值进行了更改时才会创建。用户设置会覆盖存储在<exename>.config文件中的默认设置。如果未进行任何更改,则不存在用户配置文件。而且...一旦它确实存在,您会注意到只有已更改的设置才会显示在 user.config 文件中。
To test this... create a dummy variable called like temp in your Settings file. When you start your application do:
为了测试这个...在您的设置文件中创建一个名为像 temp 的虚拟变量。当你启动应用程序做的:
Settings1.Default.temp = Settings1.Default.temp + 1;
Settings1.Default.Save();
You will now have a file called user.config created in the user's ApplicationData folder which on Vista is in: C:\Users\<username>\AppData\Local\<company>\<productname>
您现在将在用户的 ApplicationData 文件夹中创建一个名为 user.config 的文件,该文件夹在 Vista 中位于: C:\Users\<username>\AppData\Local\<company>\<productname>
Here is some code I wrote to help identify where all the various SpecialFolders where on different Operating Systems. (Might want to do a find-replace for log.Debugand replace with Console.WriteLine)
这是我编写的一些代码,用于帮助确定不同操作系统上所有各种特殊文件夹的位置。(可能想要对 进行查找替换log.Debug并替换为Console.WriteLine)
log.Debug("SpecialFolder.ApplicationData: " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData ));
log.Debug("SpecialFolder.CommonApplicationData: " + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData ));
log.Debug("SpecialFolder.ProgramFiles: " + Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
log.Debug("SpecialFolder.CommonProgramFiles: " + Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles ));
log.Debug("SpecialFolder.DesktopDirectory: " + Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory ));
log.Debug("SpecialFolder.LocalApplicationData: " + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData ));
log.Debug("SpecialFolder.MyDocuments: " + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments ));
log.Debug("SpecialFolder.System: " + Environment.GetFolderPath(Environment.SpecialFolder.System ));
Output On Windows Server 2003:
Windows Server 2003 上的输出:
SpecialFolder.ApplicationData: "C:\Documents and Settings\blake\Application Data"
SpecialFolder.CommonApplicationData: "C:\Documents and Settings\All Users\Application Data"
SpecialFolder.ProgramFiles: "C:\Program Files"
SpecialFolder.CommonProgramFiles: "C:\Program Files\Common Files"
SpecialFolder.DesktopDirectory: "C:\Documents and Settings\blake\Desktop"
SpecialFolder.LocalApplicationData: "C:\Documents and Settings\blake\Local Settings\Application Data"
SpecialFolder.MyDocuments: "C:\Documents and Settings\blake\My Documents"
SpecialFolder.System: "C:\WINDOWS\system32"
Output on Vista:
Vista上的输出:
SpecialFolder.ApplicationData: "C:\Users\blake\AppData\Roaming"
SpecialFolder.CommonApplicationData: "C:\ProgramData"
SpecialFolder.ProgramFiles: "C:\Program Files"
SpecialFolder.CommonProgramFiles: "C:\Program Files\Common Files"
SpecialFolder.DesktopDirectory: "C:\Users\blake\Desktop"
SpecialFolder.LocalApplicationData: "C:\Users\blake\AppData\Local"
SpecialFolder.MyDocuments: "C:\Users\blake\Documents"
SpecialFolder.System: "C:\Windows\system32"
回答by meklarian
The app.config file represents settings intrinsic to the application, and will apply to all sessions of the application. The *.settings file is typically for settings specific to a given user's use of an application.
Try looking in the Application Data directory beneath Local Settings [?username?\Local Settings\Application Data\?app name ...?], instead of the user's direct Application Data directory [?username?\Application Data].
The copy of the settings in the app.config file for a given *.settings file are used to initialize it the first time those settings are used; including the case when a new setting is added and a prior copy of the *.settings file doesn't have the new setting yet.
app.config 文件代表应用程序固有的设置,并将应用于应用程序的所有会话。*.settings 文件通常用于特定于给定用户使用应用程序的设置。
尝试查看本地设置 [?username?\Local Settings\Application Data\?app name ...?] 下的 Application Data 目录,而不是用户的直接 Application Data 目录 [?username?\Application Data]。
给定 *.settings 文件的 app.config 文件中的设置副本用于在第一次使用这些设置时对其进行初始化;包括添加新设置并且 *.settings 文件的先前副本还没有新设置的情况。
Here is a link for a brief walk-through of settings in C#.
这是一个链接,用于简要介绍 C# 中的设置。

