我无法按钮做两件事

时间:2016-03-08 10:55:56

标签: c# button

基本上我想要一个按钮来创建.cnf文件并一个接一个地运行.bat文件。

代码1 :(按钮1)(非完整代码)

private void button6_Click(object sender, EventArgs e)
{
    string path = @"C:\OpenSSL-Win64\bin\"+(serverListBox.SelectedItem)+".cnf";

    using (StreamWriter sw = File.CreateText(path))
    {
        sw.WriteLine("localityName_default        =" + (localityTextBox.Text));
        sw.WriteLine("0.organizationName_default  =" + (organisationTextBox.Text));
        sw.WriteLine("organizationalUnitName_default  =" + (organisationalUnitTextBox.Text));
        sw.WriteLine("commonName_default          =" + (domainListBox.SelectedItem));
        sw.WriteLine("emailAddress_default            =" + (emailTextBox.Text));
    }
}

代码2:(Button2)

private void button7_Click(object sender, EventArgs e)
{
    string path = @"C:\OpenSSL-Win64\bin\"+(serverListBox.SelectedItem)+".bat";

    //This Saves The Users Data into a text file
    using (StreamWriter sw = File.CreateText(path))
    {
        sw.WriteLine(@"@cd\OpenSSL-Win64\bin");
        sw.WriteLine(@"@dir");
        sw.WriteLine(@"openssl genrsa -out "+(serverListBox.SelectedItem)+".key 2048");
        sw.WriteLine("");
        sw.WriteLine(@"@cd\openSSL-Win64\bin");
        sw.WriteLine(@"@dir");
        sw.WriteLine(@"openssl req -new -nodes -key "+(serverListBox.SelectedItem)+".key -out "+(serverListBox.SelectedItem)+".csr -config "+(serverListBox.SelectedItem)+".cnf -batch");
    }

    Process GenKey = new Process();
    GenKey.StartInfo.FileName = @"C:\OpenSSL-Win64\bin\"+(serverListBox.SelectedItem)+".bat";
    GenKey.Start();
}

第一段代码创建.cnf文件,第二段代码运行.bat文件。这很完美,但你必须按下两个按钮,一个接一个。我希望只需按一下按钮即可工作。

2 个答案:

答案 0 :(得分:0)

您可以使用代理

this.button1.Click += new System.EventHandler(this.button1_Click);
this.button1.Click += new System.EventHandler(this.button2_Click);

在Designer.cs文件中修改它。

答案 1 :(得分:0)

button6_Clickbutton7_Click是单击按钮时调用的方法(它们处理按钮控件的单击事件)。如果您根本不需要两个单独的按钮,则可以创建不与按钮控件链接的方法:

private void CreateConfig()
{
    string path = @"C:\OpenSSL-Win64\bin\"+(serverListBox.SelectedItem)+".cnf";

    using (StreamWriter sw = File.CreateText(path))
    {
        sw.WriteLine("localityName_default        ="+(localityTextBox.Text));
        sw.WriteLine("0.organizationName_default  ="+(organisationTextBox.Text));
        sw.WriteLine("organizationalUnitName_default  =" +(organisationalUnitTextBox.Text));
        sw.WriteLine("commonName_default          =" +(domainListBox.SelectedItem));
        sw.WriteLine("emailAddress_default            ="+(emailTextBox.Text));
    }
}

类似于您创建批处理文件。然后,您可以在其中一个按钮单击处理程序中调用它们:

private void button6_Click(object sender, EventArgs e)
{
    CreateConfig();
    CreateBatchFile();
}