C# Properties.Settings.Default 的数据保存在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1804302/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:59:54  来源:igfitidea点击:

Where is the data for Properties.Settings.Default saved?

c#wpfsettings

提问by Edward Tanguay

In my WPF application, I click on Settings.settingsin the Solution Explorer and enter a StringCollectionvariable with a Userscope:

在我的 WPF 应用程序中,我单击解决方案资源管理器中的Settings.settings并输入一个具有用户范围的StringCollection变量:

alt text

替代文字

in my app.config I see that they are saved there:

在我的 app.config 中,我看到它们保存在那里:

<userSettings>
    <TestSettings.Properties.Settings>
        <setting name="Paths" serializeAs="Xml">
            <value>
                <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                    <string>one</string>
                    <string>two</string>
                    <string>three</string>
                    <string>four</string>
                    <string>five</string>
                    <string>six</string>
                    <string>seven</string>
                </ArrayOfString>
            </value>
        </setting>
    </TestSettings.Properties.Settings>
</userSettings>

then I run my applicationand with this code:

然后我运行我的应用程序并使用以下代码:

StringCollection paths = Properties.Settings.Default.Paths;

Properties.Settings.Default.Paths.Add("added in code");
Properties.Settings.Default.Save();

foreach (var path in paths)
{
    System.Console.WriteLine(path);
}

which gives me this output:

这给了我这个输出

one
two
three
four
five
six
seven
added in code

I run the application againand it gives me this output:

再次运行应用程序,它给了我这个输出:

one
two
three
four
five
six
seven
added in code
added in code

But I look at my app.configagain and it still has the original values:

但是我再次查看我的app.config,它仍然具有原始值

<userSettings>
    <TestSettings.Properties.Settings>
        <setting name="Paths" serializeAs="Xml">
            <value>
                <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                    <string>one</string>
                    <string>two</string>
                    <string>three</string>
                    <string>four</string>
                    <string>five</string>
                    <string>six</string>
                    <string>seven</string>
                </ArrayOfString>
            </value>
        </setting>
    </TestSettings.Properties.Settings>
</userSettings>

Where are the values that are added by the application being saved?

应用程序添加的值在哪里保存?

采纳答案by Jo?o Angelo

Since you selected user scope, they are saved in each user profile directory, more specifically, inside the AppDatafolder of the user profile in a file named user.config.

由于您选择了用户范围,因此它们保存在每个用户配置文件目录中,更具体地说,保存在用户配置AppData文件文件夹中名为user.config.

The full path is dependent of the application.

完整路径取决于应用程序。

In Windows 7 without roaming profile and with an Windows Forms Application named Example.Settings.CustomClassI'm getting the following folder:

在没有漫游配置文件和名为 Windows 窗体应用程序的 Windows 7 中,Example.Settings.CustomClass我得到以下文件夹:

C:\Users\[user]\AppData\Local\Microsoft\Example.Settings.CustomCl_Url_3qoqzcgn1lbyw2zx3oz1o3rsw2anyjsn.0.0.0

Also note that they are saved taking in consideration the version of your application and that the values stored in App.configare the default values used for a new user.

另请注意,它们的保存考虑了您的应用程序版本,并且存储App.config的值是用于新用户的默认值。

回答by Markus

I was looking under Win 10 for the Settings. If anyone else need to know, they are not stored in Subfolder of Microsoft (see previous answer). Just look here:

我在 Win 10 下寻找设置。如果其他人需要知道,它们不会存储在 Microsoft 的子文件夹中(请参阅上一个答案)。看看这里:

C:\Users\[user]\AppData\Local\Example\Example....0.0.0\

回答by Jason Snelders

I stumbled across an easy way to find the path(s).

我偶然发现了一种找到路径的简单方法。

  1. Open the Application Properties.
  2. Under the "Application" tab, select "Assembly Information...".
  3. Change the value of "Company". Select OK to save.
  4. Select the "Settings" application properties tab.
  5. Select "Synchronize" (first button in the options at the top of the tab).
    You should then receive a Visual Studio information dialogue saying "No user.config files were found in any of the following locations:" followed by a list of locations where the settings are saved.
  1. 打开应用程序属性。
  2. 在“应用程序”选项卡下,选择“程序集信息...”。
  3. 更改“公司”的值。选择确定以保存。
  4. 选择“设置”应用程序属性选项卡。
  5. 选择“同步”(选项卡顶部选项中的第一个按钮)。
    然后,您应该会收到一个 Visual Studio 信息对话框,提示“在以下任何位置均未找到 user.config 文件:”,然后是保存设置的位置列表。