© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
3 replies
Full-Stack Mike

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);
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Minimizing WPF app to system tray?
C#CC# / help
3y ago
✅ tray system
C#CC# / help
3y ago
❔ minimize to tray
C#CC# / help
3y ago
✅ Minimize to tray
C#CC# / help
3y ago