简体   繁体   English

从另一个应用程序更改app.config

[英]Changing app.config from another application

I made an application that is able to change its app.config (the connection string part). 我制作了一个能够更改其app.config(连接字符串部分)的应用程序。 I tried several solutions and this proved to be the easiest way to solve one of my problems. 我尝试了几种解决方案,这被证明是解决我的问题之一的最简单方法。 This is the code I use: 这是我使用的代码:

ConnectionStringSettings postavke = new ConnectionStringSettings("Kontrolor.Properties.Settings.KontrolorConnectionString", constring);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings.Clear();
config.ConnectionStrings.ConnectionStrings.Add(postavke);   

config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection(config.ConnectionStrings.SectionInformation.SectionName);

This code is placed inside a button_click method, and when I click that button and restart the application the changes are visible. 这段代码位于button_click方法中,当我单击该按钮并重新启动应用程序时,更改可见。 My question is this - is there a way to do it from another (independent) application that would enable the user to create the connection string by entering the required values into a textBox or selecting it from comboBox (he needs only to enter the IP of the server and the name of the database). 我的问题是-有没有办法从另一个(独立的)应用程序中执行此操作,该应用程序使用户可以通过在textBox中输入所需的值或从comboBox中选择所需的值来创建连接字符串(他只需输入IP的IP服务器和数据库的名称)。 By doing that, the first application would be preprepared and there would be no need to restart it to apply changes. 这样,将准备第一个应用程序,并且无需重新启动它即可应用更改。 Is there a way to do this? 有没有办法做到这一点?

Since both applications are on same machine you can use simple windows messaging, register windows message in both applications and sender post message to receiver, here is the example code: 由于两个应用程序都在同一台计算机上,因此您可以使用简单的Windows消息传递,在两个应用程序中注册Windows消息,并将发件人向接收者发布消息,这是示例代码:

Sender : 发件人:

  public partial class FormSender : Form
  {
    [DllImport("user32")]
    private static extern int RegisterWindowMessage(string message);

    private static readonly int WM_REFRESH_CONFIGURATION = RegisterWindowMessage("WM_REFRESH_CONFIGURATION");

    [DllImport("user32")]
    private static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);

    public FormSender()
    {
      InitializeComponent();
    }

    private void btnNotify_Click(object sender, EventArgs e)
    {
      NotifyOtherApp();
    }

    private void NotifyOtherApp()
    {
      List<Process> procs = Process.GetProcesses().ToList();
      Process receiverProc = procs.Find(pp => pp.ProcessName == "Receiver" || pp.ProcessName == "Receiver.vshost");  
      if (receiverProc != null)
        PostMessage((IntPtr)receiverProc.MainWindowHandle, WM_REFRESH_CONFIGURATION, new IntPtr(0), new IntPtr(0));
    }
  }

Receiver : 接收方:

 public partial class FormReceiver : Form
  {
    [DllImport("user32")]
    private static extern int RegisterWindowMessage(string message);

    private static readonly int WM_REFRESH_CONFIGURATION = RegisterWindowMessage("WM_REFRESH_CONFIGURATION");

    public FormReceiver()
    {
      InitializeComponent();
    }

    protected override void WndProc(ref Message m)
    {
      if (m.Msg == WM_REFRESH_CONFIGURATION)
      {
        lblMessageReceived.Text = "Refresh message recevied : " + DateTime.Now.ToString();
      }
      else
      {
        base.WndProc(ref m);
      }
    }
  }

btw. 顺便说一句 note that I am checking for process name "Receiver.vshost" so that it can work when started in VS debugger 请注意,我正在检查进程名称“ Receiver.vshost”,以便在VS调试器中启动时可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM