내가 만든 여러 프로젝트를 설치하는 설치 프로그램 프로젝트를 만들었습니다. 버튼을 클릭하면 다른 프로그램을 여는 메인 창이 있습니다. 사용자가 버튼 중 하나를 클릭 할 때 mainWindow와 프로그램 (문자열 값) 사이의 데이터를 구문 분석하고 싶습니다. 프로세스를 사용하여 설치 프로그램이 응용 프로그램 폴더에 설치 한 프로그램을 시작합니다.
Process OpenProject1 = Process.Start(".\\" + "Project1.exe", "StringToParseHere");
어떻게 할 수 있습니까?
미리 감사드립니다 :)
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);
}
}
}
}
say에 들어갈 인수 값이 필요한 경우 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 Form의 기본 클래스 생성자에 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;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다