将应用程序设置存储在项目文件夹而不是AppData中

我的项目中有一个Settings.cs文件,我从程序中访问其中的数据

Properties.Settings.Default.MyProperty 

生成的设置文件存储在以下位置

 C:\Users\Foo\AppData\Local\MyApp\MyApp.exe_Url_jknwq2raeohczydfp1loj02nf05zldfk\1.0.0.0\user.config 

问题是这不仅是用户特定的,而且还导致程序为每个签名(调试/发布等)提供了许多user.config文件,这迫使开发人员用户每次都重新填充整个设置他启动了一个没有特定user.config的程序的“版本”。 (如果我不够清楚,我会很乐意提供更多细节)

我希望我的应用程序为所有用户提供单个设置文件,无论“版本”(调试/发布,否则)。 这样,开发用户必须一次设置设置,并且这些设置在每次启动应用程序时都有效,而无需为其他签名/用户重新输入它们。

只需使用具有应用程序范围的设置。

您可以像注册表中的所有高级程序一样保存和读取设置,这是如何做到的:

 public object GetRegistryValue(string KeyName, object DefaultValue) { object res = null; try { Microsoft.VisualBasic.Devices.Computer c = new Microsoft.VisualBasic.Devices.Computer(); Microsoft.Win32.RegistryKey k = c.Registry.CurrentUser.OpenSubKey("Software\\YourAppName", true); if (k != null) { res = k.GetValue(KeyName, DefaultValue); } else { k = c.Registry.CurrentUser.CreateSubKey("Software\\YourAppName"); } if (k != null) k.Close(); // ex As Exception } catch { //PromptMsg(ex) } return res; } public void SetRegistryValue(string KeyName, object _Value) { try { Microsoft.VisualBasic.Devices.Computer c = new Microsoft.VisualBasic.Devices.Computer(); Microsoft.Win32.RegistryKey k = c.Registry.CurrentUser.OpenSubKey("Software\\YourAppName", true); if (k != null) { k.SetValue(KeyName, _Value); } else { k = c.Registry.CurrentUser.CreateSubKey("Software\\YourAppName"); k.SetValue(KeyName, _Value); } if (k != null) k.Close(); // ex As Exception } catch { //PromptMsg(ex) } } 

另一个选择是你创建一个可序列化的类( [Serializable()] attrib),其中包含所有设置作为属性,然后使用BinaryFormatter类将其保存在app目录中。

 public void saveBinary(object c, string filepath) { try { using (System.IO.Stream sr = System.IO.File.Open(filepath, System.IO.FileMode.Create)) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); bf.Serialize(sr, c); sr.Close(); } } catch (Exception ex) { throw ex; } } public object loadBinary(string path) { try { if (System.IO.File.Exists(path)) { using (System.IO.Stream sr = System.IO.File.Open(path, System.IO.FileMode.Open)) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); object c = bf.Deserialize(sr); sr.Close(); return c; } } else { throw new Exception("File not found"); } } catch (Exception ex) { throw ex; } return null; } 

我设计了一个简单的解决方案,但有一些缺点:

 public void WriteLocalValue(string localKey, string curValue) { Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); KeyValueConfigurationElement k = config.AppSettings.Settings[localKey]; if (k == null) config.AppSettings.Settings.Add(localKey, curValue); else k.Value = curValue; config.Save(); } public string ReadLocalValue(string localKey, string defValue) { string v = defValue; try { Configuration config = ConfigurationManager.OpenExeConfiguration( Application.ExecutablePath); KeyValueConfigurationElement k = config.AppSettings.Settings[localKey]; if (k != null) v = (k.Value == null ? defValue : k.Value); return v; } catch { return defValue; } } 

问题:您需要UAC assense来覆盖您的可执行配置,并且您不能使用Properties.Settings.Default.MyProperty语法。

我终于选择了一个更简单直接的替代方案,包括创建一个普通的Settings类,并按照此处的描述对其进行序列化/反序列化