如何在应用程序重新启动后运行命令

时间:2015-05-10 17:34:46

标签: c#

我正在尝试重新启动应用程序并运行命令。例如,当用户单击他想要的语言时,它将被检查为true,并忽略另一个带有checked = false的语言。完成后,应用程序将重新启动并检查用户在重新启动后检查的语言并获取语言。

   public Application()
    {
        InitializeComponent();

        check_language();
        languages();
    }

    private void lang_english_Click(object sender, EventArgs e)
    {

        // problem *******

        Application.Restart();

        // if i remove this is works ok.
        // when app is restarted it is like starting it so i dont think
        // this works at all. is there an other way to read this?
        // maybe with a bool?

        lang_english.Checked = true;
        //Ignore
        lang_portuguese.Checked = false;

        MessageBox.Show("Language was set to English.\r\nCliente will now restart.", "Language", MessageBoxButtons.OK, MessageBoxIcon.Information);
        check_language();
    }

    private void lang_portuguese_Click(object sender, EventArgs e)
    {
        lang_portuguese.Checked = true;
        //Ignore
        lang_english.Checked = false;

        MessageBox.Show("Language was set to Portuguese.\r\nCliente will now restart.", "Language", MessageBoxButtons.OK, MessageBoxIcon.Information);
        check_language();
    }

    private void languages()
    {
        //Languages
        }
    }

    private void check_language()
    {
        if (lang_english.Checked == true)
        {
            languages(); //Get the languages

            //Ignore
            lang_portuguese.Checked = false;
        }
        else if (lang_portuguese.Checked == true)
        {
            languages(); //Get the languages

            //Ignore
            lang_english.Checked = false;
        }
    }

2 个答案:

答案 0 :(得分:0)

首先我将所选语言保存在app.config文件中,您可以在启动时检查配置并检查相应的语言。

对于重新启动应用程序,我将使用以下两个选项之一:

1)Application.Restart()

2)启动第二个应用程序然后结束第一个应用程序。请参阅此帖子:Restart WinForms Application

您可能还希望在运行时更改语言Change language at run-time

答案 1 :(得分:0)

在SAME应用程序控件中重新启动应用程序后,您正在检查语言。您应该在文件中编写和阅读该语言,以便在那里进行检查:

private void lang_portuguese_Click(object sender, EventArgs e)
{
new System.IO.StreamWriter(new System.IO.FileStream("File.ext", System.IO.FileMode.Create)).Write("Portuguese");
}

private void check_language()
{
String lang = new System.IO.StreamReader("YouFile.ext").ReadLine();
    if (lang == "English")
    {
        languages(); //Get the languages

        //Ignore
        lang_portuguese.Checked = false;
    }
    else if (lang == "Portuguese")
    {
        languages(); //Get the languages

        //Ignore
        lang_english.Checked = false;
    }
}

这是示例。您应该使用验证进行编写和读取并创建流的实例,以便在完成读/写后关闭它。 这是我能给你的想法。