0

I made an application that is able to change its app.config (the connection string part). 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. 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). 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?

4

1 に答える 1

1

両方のアプリケーションが同じマシン上にあるため、単純な Windows メッセージングを使用できます。Windows メッセージを両方のアプリケーションに登録し、送信者がメッセージを受信者に投稿します。コード例は次のとおりです。

送信者:

  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));
    }
  }

レシーバー:

 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);
      }
    }
  }

ところで。VSデバッガーで起動したときに機能するように、プロセス名「Receiver.vshost」をチェックしていることに注意してください

于 2012-04-16T12:43:18.077 に答える