private void installButton_Click(object sender, EventArgs e)
{
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
richTextBox_OK.Text = "";
richTextBox_ERROR.Text = "";
string dirPath = folderBrowserDialog.SelectedPath + "\\AVUC";
string fileExePath = folderBrowserDialog.SelectedPath + "\\AVUC\\AVUC_Microsoft_Version_Controller.exe";
string fileBatPath = "C:\\Users\\" + userName + "\\AppData\\Roaming\\Microsoft\\Windows" +
"\\Start Menu\\Programs\\Startup\\AVUC Microsoft Version Controller.bat";
string fileTxtPath = folderBrowserDialog.SelectedPath + "\\AVUC\\configLocal.txt";
try
{
richTextBox_OK.Text += "UserName: " + userName + "\n";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
richTextBox_OK.Text += "Directory \"" + dirPath + "\" created\n";
}
else
{
richTextBox_ERROR.Text += "ERROR!\tDirectory \"" + dirPath + "\" is already exists\n";
richTextBox_ERROR.Text += "NOT INSTALLED\n";
return;
}
File.Copy("AVUC Microsoft Version Controller.exe", fileExePath);
richTextBox_OK.Text += "File \"AVUC Microsoft Version Controller.exe\" copy created\n";
File.Copy("configLocal.txt", fileTxtPath);
richTextBox_OK.Text += "File \"configLocal.txt\" copy created\n";
File.WriteAllText("AVUC Microsoft Version Controller.bat", "@echo off\n" +
"cd \"" + dirPath + "\"\n" +
"start AVUC_Microsoft_Version_Controller.exe\n" +
"exit");
File.Copy("AVUC Microsoft Version Controller.bat", fileBatPath);
richTextBox_OK.Text += "\tINSTALLED\n";
}
catch (Exception ex)
{
richTextBox_ERROR.Text += "ERROR!\t" + ex.Message + "\n";
richTextBox_ERROR.Text += "\tNOT INSTALLED\n";
}
}
}
RegistryKey reg;
reg = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\");
reg.SetValue("AVUC Microsoft Version Controller", fileExePath);
reg.Close();
Thread.Sleep(60000);
File.AppendAllText("logs.txt", DateTime.Now.ToString() + "\t\t\tStarted!\n");
Подписывался на событие из самого класса окна, в это и была проблема.
Надо было сделать ViewModel для окна и оттуда подписываться на событие