如何部署默认应用程序设置

时间:2014-10-21 14:32:32

标签: c# .net settings

您好我需要为每个用户提供一些默认设置 - 字符串列表 那么每个用户都可以添加新的和sav?

我正在使用用户范围设置。

我有什么选择?

更新:

我甚至无法在调试文件夹中的文件中找到默认设置。所以问题是我需要将tp部署到用户机器

2 个答案:

答案 0 :(得分:0)

我会将类添加为AppSetting并使其序列化。 然后我将添加一个ApplicationSettingManager,它将查看像Environment.SpecialFolder.ApplicationData这样的文件夹中的默认路径,如果设置文件在那里,它将使用它来反序列化AppSetting,否则它将保存默认设置(你可以设置默认值)在AppSetting类中设置,或者您可以使用.exe旁边的app.config文件来设置默认设置)。每当用户更改设置时,ApplicationSettingManager将覆盖设置文件。

   string ApplicationDataFolder
        {
            get
            {
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                        Assembly.GetExecutingAssembly().GetName().Name);
            }
        }
        string UserSettingFilePath
        {
            get
            {
                return Path.Combine(ApplicationDataFolder, "Setting.xml");
            }
        }
 [Serializable]
    public class AppSetting
    {
        public AppSetting()
        {
           //default instantiation 
        }

        public int? CurrentUserId
        {
            get;
            set;
        }


    }
    enter code he

查看http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx您可以使用的不同路径

答案 1 :(得分:0)

您可以使用属于Microsoft.VisualBasic库的Settings object

在C#中,您可以通过以下方式访问这些:

Properties.Settings.Default.FirstUserSetting = "abc";

然后将它们保存到磁盘:

Properties.Settings.Default.Save();

这会将它们存储在Users\[UserName]\AppData\Local

下的特定于应用程序的目录中

或者,您可以自行管理序列化并将其写入Environment.SpecialFolder.ApplicationDataEnvironment.SpecialFolder.LocalApplicationData