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

时间:2022-08-06 20:21:39

I am trying to restart my application and run a command. For example, when the user clicks the language he wants it goes checked true and ignores the other one with checked = false. When that is done, the application restarts and checks what language the users checked after the restart and gets the language.

我正在尝试重新启动我的应用程序并运行命令。例如,当用户单击他想要的语言时,它将被检查为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 个解决方案

#1


First I would save the selected language in the app.config file, the you can check the config on start-up and check the appropriate language.

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

Second for restarting the application I would use one of two options:

其次,为了重新启动应用程序,我将使用以下两个选项之一:

1) Application.Restart()

2) Start a second application and then end the first. See this post: Restart WinForms Application

2)启动第二个应用程序然后结束第一个应用程序。看到这篇文章:重新启动WinForms应用程序

You may also want to look into changing the language at run-time Change language at run-time

您可能还希望在运行时以运行时更改语言来更改语言

#2


You are checking your language AFTER restarting the application inside the SAME application controls. You should write and read the language in a file so you can check it in there:

在重新启动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;
    }
}

This are EXAMPLES. you should write and read with validation and creating instances of your stream, so you close it after finishing Reading/writting. This is the idea I can give you.

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

#1


First I would save the selected language in the app.config file, the you can check the config on start-up and check the appropriate language.

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

Second for restarting the application I would use one of two options:

其次,为了重新启动应用程序,我将使用以下两个选项之一:

1) Application.Restart()

2) Start a second application and then end the first. See this post: Restart WinForms Application

2)启动第二个应用程序然后结束第一个应用程序。看到这篇文章:重新启动WinForms应用程序

You may also want to look into changing the language at run-time Change language at run-time

您可能还希望在运行时以运行时更改语言来更改语言

#2


You are checking your language AFTER restarting the application inside the SAME application controls. You should write and read the language in a file so you can check it in there:

在重新启动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;
    }
}

This are EXAMPLES. you should write and read with validation and creating instances of your stream, so you close it after finishing Reading/writting. This is the idea I can give you.

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