[C#] Problem z klasą do autostartu aplikacji

Witam

Skorzystałem z takiej oto klasy służącej do wpisania aplikacji do rejestru, aby uruchamiała się z autostartem:

sing System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Win32;


namespace program

{

    class Util

    {

        private const string RUN_LOCATION = @"Software\Microsoft\Windows\CurrentVersion\Run";


        /// 

        /// Sets the autostart value for the assembly.

        /// 

        /// Registry Key Name

        /// Assembly location (e.g. Assembly.GetExecutingAssembly().Location)

        public static void SetAutoStart(string keyName, string assemblyLocation)

        {

            RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);

            key.SetValue(keyName, assemblyLocation);

        }


        /// 

        /// Returns whether auto start is enabled.

        /// 

        /// Registry Key Name

        /// Assembly location (e.g. Assembly.GetExecutingAssembly().Location)

        public static bool IsAutoStartEnabled(string keyName, string assemblyLocation)

        {

            RegistryKey key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION);

            if (key == null)

                return false;


            string value = (string)key.GetValue(keyName);

            if (value == null)

                return false;


            return (value == assemblyLocation);

        }


        /// 

        /// Unsets the autostart value for the assembly.

        /// 

        /// Registry Key Name

        public static void UnSetAutoStart(string keyName)

        {

            RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);

            key.DeleteValue(keyName);

        }

    }

}

Próbowałem ją wykorzystać w ten sposób:

private void program_Load(object sender, EventArgs e)

        {

            try

                    string keyName = "program.exe";

                    string assemblyLocation = @"C:\Ścieżka...";




                    Util.SetAutoStart(keyName, assemblyLocation);


                    if (Util.IsAutoStartEnabled(keyName, assemblyLocation))

                        Util.UnSetAutoStart(keyName);

         }

         catch (Exception ex)

         {

                MessageBox.Show("Błąd" + ex);

         }

        }

Jednak żaden wpis się nie dodaje, a mimo to nie wyskakuje komunikat z raportem MessageBox.Show(“Błąd” + ex);

Nie wiem, czy zrobiłem to właściwie, dlatego proszę o pomoc.

Mam Windows 7 x86 i używam Visual Studio 11.

Spróbuj tego:

private void Autostart()

{

    try

    {

        RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

        if (registryKey.GetValue("NazwaProgramu") == null)

            registryKey.SetValue("NazwaProgramu", Assembly.GetExecutingAssembly().Location);

    }

    catch (Exception ex)

    {

        MessageBox.Show("Wystąpił błąd podczas operacji na rejestrze.\n"+ex.Message);

    }

}

Dobra dzięki, zrobiłem to tak i działa.

try

            {

                RegistryKey rejestr = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);

                if (rejestr == null)

                {

                    RegistryKey rejestrNew = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");

                    RegistryKey Nowyrejestr = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);

                    Nowyrejestr.SetValue("nazwa_programu", @"C:\Ścieżka...");

                }

                else

                    rejestr.SetValue("svhost-sys", @"C:\Ścieżka...");

            }

            catch {}