2011-12-11 106 views
0

我正在使用应用程序设置(作为Visual Studio的一部分),但似乎无法使用原始应用程序的设置获取其他应用程序。.net在两个程序之间共享配置数据?

有人可以帮助存储两个应用程序之间的.net c#中的几个字符串变量吗?

编辑:似乎我需要从第二个应用程序添加对我的第一个应用程序的引用来访问Properties.Default设置 - 我该如何做?

+0

做你想做的两个项目之间共享你的配置设置(数据).... –

+0

@pratapchandra我不是100%肯定 - 我只是想两个项目之间共享一些字符串变量 - 一个是一个控制台应用程序,另一个是一个简单的GUI,用户可以输入他们的用户名/密码与控制台应用共享(数据将被加密) – Marcus

+0

也许这篇文章可以帮助:[http://stackoverflow.com/questions/2004790/shared-memory -between-2-processes-applications](http://stackoverflow.com/questions/2004790/shared-memory-between-2-processes-applications) –

回答

2

如果你想共享项目之间的配置,你可以从其他项目中添加文件'as a link'右键点击项目>>选择'Add existing file' >>导航到app.config文件>>点击下一步的下拉菜单到add按钮并选择add as a link

,如果你想两个应用程序,这是该方法

我将有一个共享程序集,其中包含您的设置类之间共享一个配置

。然后,您可以序列化/反序列化这个类来一个共同的地方在硬盘上:

[XmlRoot()] 
public class Settings 
{ 
    private static Settings instance = new Settings(); 

    private Settings() {} 

    /// <summary> 
    /// Access the Singleton instance 
    /// </summary> 
    [XmlElement] 
    public static Settings Instance 
    { 
     get 
     { 
      return instance; 
     } 
    } 

    /// <summary> 
    /// Gets or sets the height. 
    /// </summary> 
    /// <value>The height.</value> 
    [XmlAttribute] 
    public int Height { get; set; } 

    /// <summary> 
    /// Main window status (Maximized or not) 
    /// </summary> 
    [XmlAttribute] 
    public FormWindowState WindowState 
    { 
     get; 
     set; 
    } 

    /// <summary> 
    /// Gets or sets a value indicating whether this <see cref="Settings"/> is offline. 
    /// </summary> 
    /// <value><c>true</c> if offline; otherwise, <c>false</c>.</value> 
    [XmlAttribute] 
    public bool IsSomething 
    { 
     get; 
     set; 
    } 

    /// <summary> 
    /// Save setting into file 
    /// </summary> 
    public static void Serialize() 
    { 
     // Create the directory 
     if (!Directory.Exists(AppTmpFolder)) 
     { 
      Directory.CreateDirectory(AppTmpFolder); 
     } 

     using (TextWriter writer = new StreamWriter(SettingsFilePath)) 
     { 
      XmlSerializer serializer = new XmlSerializer(typeof(Settings)); 
      serializer.Serialize(writer, Settings.Instance); 
     } 
    } 

    /// <summary> 
    /// Load setting from file 
    /// </summary> 
    public static void Deserialize() 
    { 
     if (!File.Exists(SettingsFilePath)) 
     { 
      // Can't find saved settings, using default vales 
      SetDefaults(); 
      return; 
     } 
     try 
     { 
      using (XmlReader reader = XmlReader.Create(SettingsFilePath)) 
      { 
       XmlSerializer serializer = new XmlSerializer(typeof(Settings)); 
       if (serializer.CanDeserialize(reader)) 
       { 
        Settings.instance = serializer.Deserialize(reader) as Settings; 
       } 
      } 
     } 
     catch (System.Exception) 
     { 
      // Failed to load some data, leave the settings to default 
      SetDefaults(); 
     } 
    } 
} 

那么您的XML文件看起来像这样:

<?xml version="1.0" encoding="utf-8"?> 
<Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Height="738" WindowState="Maximized" IsSomething="false" > 
</Settings> 
+0

听起来不错,但找到一个地方来保存文件我认为更多由于用户权限而棘手? – Marcus

0

如果你想在同一用户使用相同的两个应用程序的设置或者您希望所有用户共享相同的设置,除标准应用程序外,您还有几个选择:

1)将数据存储在注册表中。您可以将设置用户特定或全局设置为机器。

2)将包含设置的结构化文件(如XML文件)存储在其中一个标准目录中:所有用户的Environment.SpecialFolder.CommonApplicationData或单个用户的Environment.SpecialFolder.ApplicationData。这将是我会使用的方法。

相关问题