代码之家  ›  专栏  ›  技术社区  ›  Joel Cochran

访问其他应用程序的设置

  •  0
  • Joel Cochran  · 技术社区  · 15 年前

    我有应用程序1和应用程序2。App2需要验证App1是否已安装,如果已安装,则需要从App1设置访问属性。

    最好的办法是什么?

    更新 首先,我很抱歉从来没有接受这个问题的答案,我知道它现在已经超过一年了,但我问了这个问题后立即偏离了方向,然后项目就改变了,等等等等。我有罪。。。

    我现在回到了上面,我仍然需要解决这个问题,但是现在应用程序是通过ClickOnce部署的,所以我不知道它们在哪里。如有任何建议,将不胜感激。我保证这次我会选择一个答案。

    2 回复  |  直到 13 年前
        1
  •  2
  •   Timores    15 年前

    ConfigurationManager.OpenExeConfiguration的文档有一个读取另一个exe的.config文件并访问AppSettings的示例。在这里:

    // Get the application path.
    string exePath = System.IO.Path.Combine(
        Environment.CurrentDirectory, "ConfigurationManager.exe");
    
    // Get the configuration file.
    System.Configuration.Configuration config =
      ConfigurationManager.OpenExeConfiguration(exePath);
    
    // Get the AppSetins section.
    AppSettingsSection appSettingSection = config.AppSettings;
    

    至于检查是否安装了App1,您可以在安装期间在注册表中写入一个值,然后在App2中检查它(并在卸载期间删除该值)。

        2
  •  0
  •   lfalin    13 年前

    这是一种痛苦,我可以告诉你很多。我发现最好的方法是序列化Settingsclass并使用XML(下面的代码)。但请先尝试此页: http://cf-bill.blogspot.com/2007/10/visual-studio-sharing-one-file-between.html

    public class Settings
    {
        public static string ConfigFile{get{return "Config.XML";}}
        public string Property1 { get; set; }
    
        /// <summary>
        /// Saves the settings to the Xml-file
        /// </summary>
        public void Save()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            using (TextWriter reader = new StreamWriter(ConfigFile))
            {
                serializer.Serialize(reader, this);
            }
        }
        /// <summary>
        /// Reloads the settings from the Xml-file
        /// </summary>
        /// <returns>Settings loaded from file</returns>
        public static Settings Load()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            using (TextReader reader = new StreamReader(ConfigFile))
            {
                return serializer.Deserialize(reader) as Settings;
            }
        }
    }