『H』『A』『V』『O』『C』
『H』『A』『V』『O』『C』
CC#
Created by 『H』『A』『V』『O』『C』 on 3/24/2024 in #help
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);
}
}
4 replies