Starting Project with Windows and Minimizing to System Tray

I would like to start my program with Windows and then minimize my program to the system tray using the C# WinForms .Net Framework. I am using the below code to start the project with Windows, but it does not open my project, it only starts it in the Task Manager. Am I doing something wrong?
private void SetStartup2()
{
try
{
string keys =
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run";
string value = "CopyThat";

if (startWithWindowsCheckBox.Checked)
{
if (Registry.GetValue(keys, value, null) == null)
{
// if key doesn't exist
using (RegistryKey key =
Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue("CopyThat", (Application.ExecutablePath));
key.Dispose();
key.Flush();
}
}
}
else
{
if (Registry.GetValue(keys, value, null) != null)
{
// if key doesn't exist
using (RegistryKey key =
Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.DeleteValue("CopyThat", false);
key.Dispose();
key.Flush();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void SetStartup2()
{
try
{
string keys =
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run";
string value = "CopyThat";

if (startWithWindowsCheckBox.Checked)
{
if (Registry.GetValue(keys, value, null) == null)
{
// if key doesn't exist
using (RegistryKey key =
Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue("CopyThat", (Application.ExecutablePath));
key.Dispose();
key.Flush();
}
}
}
else
{
if (Registry.GetValue(keys, value, null) != null)
{
// if key doesn't exist
using (RegistryKey key =
Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.DeleteValue("CopyThat", false);
key.Dispose();
key.Flush();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
2 Replies
『H』『A』『V』『O』『C』
I know that I have not added any code to minimize it to the system tray, which I will do on the Form_Load event. I am just trying to get it to show the main form when Windows starts. I am using the latest .Net Framework with Windows 11 Home.
Stefanidze
Stefanidze9mo ago
This seems overcomplicated. All you need to do is create a link in the autostart folder, that will pass an argument to your app to start minimized
Want results from more Discord servers?
Add your server