AutoStart.AutoStartHelper.AutoStart C# (CSharp) Method

AutoStart() public static method

public static AutoStart ( string appName, string exePath, bool autoStart ) : void
appName string
exePath string
autoStart bool
return void
        public static void AutoStart(string appName, string exePath, bool autoStart)
        {
            try
            {
                RegistryKey registryKey =
                    Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true) ??
                    Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                if (registryKey != null)
                {
                    if (autoStart)
                    {
                        try
                        {
                            if (registryKey.GetValue(appName) == null)
                            {
                                registryKey.SetValue(appName, exePath);
                            }
                        }
                        catch
                        {
                            //log
                        }
                    }
                    else
                    {
                        try
                        {
                            if (registryKey.GetValue(appName) != null)
                            {
                                registryKey.DeleteValue(appName);
                            }
                        }
                        catch
                        {
                            //log
                        }
                    }

                    registryKey.Close();
                }
            }
            catch
            {
                //log
            }
        }
    }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            if (args.Length == 3)
            {
                var name = string.Empty;
                var file = string.Empty;
                var auto = -1;
                foreach (var s in args)
                {
                    var keyAndValue = s.Split('=');
                    if (keyAndValue.Length == 2)
                    {
                        if (keyAndValue[0].ToLower() == "/name")
                        {
                            name = keyAndValue[1];
                        }
                        if (keyAndValue[0].ToLower() == "/file")
                        {
                            file = keyAndValue[1];
                        }
                        if (keyAndValue[0].ToLower() == "/auto")
                        {
                            int.TryParse(keyAndValue[1], out auto);
                        }
                    }
                }

                if (!string.IsNullOrEmpty(name) && File.Exists(file) && (auto == 0 || auto == 1))
                {
                    try
                    {
                        AutoStartHelper.AutoStart(name, file, auto == 1);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
            }
        }
AutoStartHelper