Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I'm trying to make my application run on startup after I wrap the project using setup from visual studio.

Here is the function responsible for putting the Registry Key and value of my installed application to Registry Editor

private void SetStartup()
{
    try
    {
        string keys =
        @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run";
        string value = "Automated";

        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("Automated", Path.GetDirectoryName
                (Application.ExecutablePath));
                key.Dispose();
                key.Flush();
            }
        }
        else
        {
            //if key Exist
        }
    }
    catch(Exception ex)
    {
        Error_Logging(ex);
    }

}


The adding of my project's registry key and value to the registry editor is working successfully after I installed my application on drive D: and run the project for the first time and close it but when I restart the pc, the project that is supposed to run isn't running.

What I have tried:

On adding project output group from setup, I selected Primary output, Debug symbols and Content Files just to make sure my .dll, .exe and other files are included on my installer.

I turned off my firewall and add my folder where I installed my project to Exclusion of Windows Defender.
Posted
Updated 4-Jan-19 17:48pm

1 solution

You wrote the path to the directory your app is in instead of the full path to the executable to launch into the registry Run key.

Get this code out of the call to SetValue and actually get the value into a variable so you can see the string that's in there:
C#
key.SetValue("Automated", Path.GetDirectoryName
                (Application.ExecutablePath)

becomes:
C#
string path = Application.ExecuteablePath;
key.SetValue("Automated", path);
 
Share this answer
 
Comments
LaysAndNetflix 5-Jan-19 1:46am    
Yes, you are right, I noticed that one too. I forgot to update this question earlier. I was able to make it work an hour ago. My approach is similar to yours. thank you so much for posting your answer. As a token of appreciation, I will mark your solution as an answer and rate it 5 stars. Thank you again.
Jed Zerna Official 2-Mar-22 5:49am    
Hi, I'm newbie here and also programming. I want to ask, where do i put the codes??? Thank you
Dave Kreskowiak 2-Mar-22 8:58am    
Open your own question with proper details about the problem you're having and the relevant code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900