如何在已安装的WinForm项目之间解析数据?

时间:2016-05-21 14:05:56

标签: c# winforms installer

我创建了一个安装程序项目,它安装了我创建的多个项目。有一个主窗口,可以通过单击按钮打开其他程序。当用户单击其中一个按钮时,我想解析mainWindow和要打开的程序之间的数据(字符串值)。 我使用进程启动安装程序已安装到应用程序文件夹的程序。

Process OpenProject1 = Process.Start(".\\" + "Project1.exe", "StringToParseHere");

我该怎么做?

提前致谢:)

1 个答案:

答案 0 :(得分:0)

  

我想解析mainWindow和要打开的程序之间的数据(字符串值)

让Project1.exe读入" StringToParseHere"当它开始时,将代码添加到主事件:

using System;

class Program
{
    static void Main(string[] args)
    {
       if (args != null)
       {
        for (int i = 0; i < args.Length; i++) // Loop through array or command line parameters
        {
            string argument = args[i];
            MessageBox.Show(argument);
        }
      }
    }
}

如果你需要参数值进入说Form1,那么创建一个重载的类构造函数并保存到私有成员变量,例如:

private string argumentParsedIn = string.empty; //This is the member variable

//base  class/form constuctor
Public Form1()
{
}

//Overloaded class/form constructor that takes a parameter
Public Form1(string argument)
{
   argumentParsedIn = argument;
}

需要注意的一点是,WinForm表单的基类构造函数具有InitializeComponent();方法。所以你的重载构造函数应该调用该方法,这样做的设计模式是,例如:

解决方案

Program.cs的

static class Program
{
    [STAThread]
    static void Main()
    {
       if (args != null)
       {
        for (int i = 0; i < args.Length; i++) // Loop through array or command line parameters
        {
            string argument = args[i];
            //MessageBox.Show(argument);
        }
      }
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        var application = new WindowsFormsApplication();
        application.Run(new Form1(argument)); //<-- see here is how I pass it
    }
}

Form1.cs的

private string argumentParsedIn = string.empty; //This is the member variable
Public Form1() : System.Windows.Forms.Form
{
   InitializeComponent();
}

Public Form1(string argument) : base() //<-- see here, adding the base will call the base constructor
{
   argumentParsedIn = argument;
}