<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Crevice.UI
{
    using Crevice.Logging;
    using Crevice.Config;

    public partial class LauncherForm : Form
    {
        private readonly MainFormBase _mainForm;

        public LauncherForm(MainFormBase mainForm)
        {
            _mainForm = mainForm;
            Icon = Properties.Resources.CreviceIcon;
            InitializeComponent();
        }

        private static Microsoft.Win32.RegistryKey AutorunRegistry()
            => Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
        
        private static bool AutoRun
        {
            get
            {
                var registry = AutorunRegistry();
                try
                {
                    var res = registry.GetValue(Application.ProductName);
                    return res != null &&
                           (string)res == Application.ExecutablePath;
                }
                finally
                {
                    registry.Close();
                }
            }
            set
            {
                if (value)
                {
                    var registry = AutorunRegistry();
                    registry.SetValue(Application.ProductName, Application.ExecutablePath);
                    Verbose.Print("Autorun was set to true");
                    registry.Close();
                }
                else
                {
                    var registry = AutorunRegistry();
                    try
                    {
                        registry.DeleteValue(Application.ProductName);
                        Verbose.Print("Autorun was set to false");
                    }
                    catch (ArgumentException ex)
                    {
                        Verbose.Error("An exception was thrown while writing registory value: {0}", ex.ToString());
                    }
                    registry.Close();
                }
            }
        }

        private void saveSettings()
        {
            AutoRun = checkBox1.Checked;
        }

        private void ShowProductInfoForm()
        {
            string[] args = { "--help" };
            var cliOption = CLIOption.Parse(args);
            var form = new ProductInfoForm(cliOption);
            form.Show();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            checkBox1.Checked = AutoRun;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            Opacity = 0;
            Show();
            var rect = Screen.PrimaryScreen.Bounds;
            Left = (rect.Width - Width) / 2;
            Top = (rect.Height - Height) / 2;
            Opacity = 1;
            Activate();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            saveSettings();
            base.OnClosing(e);
        }
<commit_msg>Fix an issue error thrown when saving autorun setting and the key is empty.<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Crevice.UI
{
    using Crevice.Logging;
    using Crevice.Config;

    public partial class LauncherForm : Form
    {
        private readonly MainFormBase _mainForm;

        public LauncherForm(MainFormBase mainForm)
        {
            _mainForm = mainForm;
            Icon = Properties.Resources.CreviceIcon;
            InitializeComponent();
        }

        private static Microsoft.Win32.RegistryKey AutorunRegistry()
            => Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
        
        private static bool AutoRun
        {
            get
            {
                var registry = AutorunRegistry();
                try
                {
                    var res = registry.GetValue(Application.ProductName);
                    return res != null &&
                           (string)res == Application.ExecutablePath;
                }
                finally
                {
                    registry.Close();
                }
            }
            set
            {
                if (value)
                {
                    var registry = AutorunRegistry();
                    registry.SetValue(Application.ProductName, Application.ExecutablePath);
                    Verbose.Print("Autorun was set to true");
                    registry.Close();
                }
                else
                {
                    var registry = AutorunRegistry();
                    if (registry.GetValue(Application.ProductName) != null)
                    {
                        try
                        {
                            registry.DeleteValue(Application.ProductName);
                            Verbose.Print("Autorun was set to false");
                        }
                        catch (ArgumentException ex)
                        {
                            Verbose.Error("An exception was thrown while writing registory value: {0}", ex.ToString());
                        }
                    }
                    registry.Close();
                }
            }
        }

        private void SaveSettings()
        {
            AutoRun = checkBox1.Checked;
        }

        private void ShowProductInfoForm()
        {
            string[] args = { "--help" };
            var cliOption = CLIOption.Parse(args);
            var form = new ProductInfoForm(cliOption);
            form.Show();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            checkBox1.Checked = AutoRun;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            Opacity = 0;
            Show();
            var rect = Screen.PrimaryScreen.Bounds;
            Left = (rect.Width - Width) / 2;
            Top = (rect.Height - Height) / 2;
            Opacity = 1;
            Activate();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            SaveSettings();
            base.OnClosing(e);
        }

{'fraction_non_alphanumeric': 0.08013884696649562, 'fraction_numerical': 0.0033202535466344703, 'mean_word_length': 1.2230795035223079, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5154503', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1510, 'n_words': 344}