<commit_before>﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
using OutlookDesktop.Properties;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32;

namespace OutlookDesktop
{
    class Startup
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// The main entry point for the application.
        /// We only only one instance of the application to be running.
        /// </summary>
        [STAThread]
        static void Main()
        {
            log.Debug("Checking to see if there is a instance running.");
            if (IsAlreadyRunning())
            {
                // let the user know the program is already running.
                log.Warn("Instance is already running, exiting.");
                MessageBox.Show(Resources.ProgramIsAlreadyRunning, Resources.ProgramIsAlreadyRunningCaption, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            else
            {

                if (!IsOutlook2000OrHigherInstalled())
                {
                    log.Error("Outlook is not avaliable or installed.");
                    MessageBox.Show("This program requires Microsoft Outlook 2000 or higher." + Environment.NewLine + "Please install Microsoft Office and try again.", "Missing Requirements", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Application.EnableVisualStyles();

                log.Debug("Starting the instance manager and loading instances.");
                InstanceManager instanceManager = new InstanceManager();
                instanceManager.LoadInstances();

                Application.Run(instanceManager);
            }
        }

        /// <summary>
        /// GetCurrentInstanceWindowHandle
        /// </summary>
        /// <returns></returns>
        private static IntPtr GetCurrentInstanceWindowHandle()
        {
            IntPtr hWnd = IntPtr.Zero;
            Process process = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(process.ProcessName);
            foreach (Process _process in processes)
            {
                // Get the first instance that is not this instance, has the
                // same process name and was started from the same file name
                // and location. Also check that the process has a valid
                // window handle in this session to filter out other user's
                // processes.
                if (_process.Id != process.Id &&
                    _process.MainModule.FileName == process.MainModule.FileName &&
                    _process.MainWindowHandle != IntPtr.Zero)
                {
                    hWnd = _process.MainWindowHandle;
                    break;
                }
            }
            return hWnd;
        }

        /// <summary>
        /// check if given exe alread running or not
        /// </summary>
        /// <returns>returns true if already running</returns>
        private static bool IsAlreadyRunning()
        {
            string strLoc = Assembly.GetExecutingAssembly().Location;
            FileSystemInfo fileInfo = new FileInfo(strLoc);
            string sExeName = fileInfo.Name;
            bool createdNew;

            mutex = new Mutex(true, "Local\\" + sExeName, out createdNew);

            return !createdNew;
        }

        static Mutex mutex;
        const int SW_RESTORE = 9;

        /// <summary>
        /// Returns the 32-bit Program Files directory depending on which Windows arch we're running on.
        /// </summary>
        /// <returns></returns>
        private static string ProgramFilesx86()
        {
            String programFilesX86Dir = string.Empty;

            if (8 == IntPtr.Size || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
            {
                programFilesX86Dir = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
            }

            programFilesX86Dir = Environment.GetEnvironmentVariable("ProgramFiles");
            log.Debug("Program Files Directory: " + programFilesX86Dir);
            return programFilesX86Dir;
        }

        /// <summary>
        /// Returns true if Outlook 2000 (or higher) is installed.
        /// </summary>
        /// <returns>New version of Office need to be explicily supported in this function.</returns>
        private static bool IsOutlook2000OrHigherInstalled()
        {
            bool hasOffice2000OrHigher = false;
            string outlookPath = string.Empty;

            // first make sure they have Office/Outlook 2000 (9.0) or higher installed by looking for 
            // the version subkeys in HKLM.
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Office"))
            {
                log.Info("Successfully read reg key: HKLM\\Software\\Microsoft\\Office");
                string[] subkeys = key.GetSubKeyNames();

                foreach (string subkey in subkeys)
                {
                    log.Info("Analyzing subkey '" + subkey + "'");
                    double versionSubKey;
                    try
                    {
                        versionSubKey = double.Parse(subkey);
                        log.Info("Office Version: " + versionSubKey.ToString());
                        if (versionSubKey > 9)
                        {
                            hasOffice2000OrHigher = true;
                            break;
                        }
                    }
                    catch (Exception ex)
                    {                        
                       log.Error("Error parsing registry: " + ex.ToString());
                    }<commit_msg>finally fixed outlook detection issue for non-english regional settings<commit_after>﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
using OutlookDesktop.Properties;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32;
using System.Globalization;

namespace OutlookDesktop
{
    class Startup
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// The main entry point for the application.
        /// We only only one instance of the application to be running.
        /// </summary>
        [STAThread]
        static void Main()
        {
            log.Debug("Checking to see if there is a instance running.");
            if (IsAlreadyRunning())
            {
                // let the user know the program is already running.
                log.Warn("Instance is already running, exiting.");
                MessageBox.Show(Resources.ProgramIsAlreadyRunning, Resources.ProgramIsAlreadyRunningCaption, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            else
            {

                if (!IsOutlook2000OrHigherInstalled())
                {
                    log.Error("Outlook is not avaliable or installed.");
                    MessageBox.Show("This program requires Microsoft Outlook 2000 or higher." + Environment.NewLine + "Please install Microsoft Office and try again.", "Missing Requirements", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Application.EnableVisualStyles();

                log.Debug("Starting the instance manager and loading instances.");
                InstanceManager instanceManager = new InstanceManager();
                instanceManager.LoadInstances();

                Application.Run(instanceManager);
            }
        }

        /// <summary>
        /// GetCurrentInstanceWindowHandle
        /// </summary>
        /// <returns></returns>
        private static IntPtr GetCurrentInstanceWindowHandle()
        {
            IntPtr hWnd = IntPtr.Zero;
            Process process = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(process.ProcessName);
            foreach (Process _process in processes)
            {
                // Get the first instance that is not this instance, has the
                // same process name and was started from the same file name
                // and location. Also check that the process has a valid
                // window handle in this session to filter out other user's
                // processes.
                if (_process.Id != process.Id &&
                    _process.MainModule.FileName == process.MainModule.FileName &&
                    _process.MainWindowHandle != IntPtr.Zero)
                {
                    hWnd = _process.MainWindowHandle;
                    break;
                }
            }
            return hWnd;
        }

        /// <summary>
        /// check if given exe alread running or not
        /// </summary>
        /// <returns>returns true if already running</returns>
        private static bool IsAlreadyRunning()
        {
            string strLoc = Assembly.GetExecutingAssembly().Location;
            FileSystemInfo fileInfo = new FileInfo(strLoc);
            string sExeName = fileInfo.Name;
            bool createdNew;

            mutex = new Mutex(true, "Local\\" + sExeName, out createdNew);

            return !createdNew;
        }

        static Mutex mutex;
        const int SW_RESTORE = 9;

        /// <summary>
        /// Returns the 32-bit Program Files directory depending on which Windows arch we're running on.
        /// </summary>
        /// <returns></returns>
        private static string ProgramFilesx86()
        {
            String programFilesX86Dir = string.Empty;

            if (8 == IntPtr.Size || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
            {
                programFilesX86Dir = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
            }

            programFilesX86Dir = Environment.GetEnvironmentVariable("ProgramFiles");
            log.Debug("Program Files Directory: " + programFilesX86Dir);
            return programFilesX86Dir;
        }

        /// <summary>
        /// Returns true if Outlook 2000 (or higher) is installed.
        /// </summary>
        /// <returns>New version of Office need to be explicily supported in this function.</returns>
        private static bool IsOutlook2000OrHigherInstalled()
        {
            bool hasOffice2000OrHigher = false;
            string outlookPath = string.Empty;

            // first make sure they have Office/Outlook 2000 (9.0) or higher installed by looking for 
            // the version subkeys in HKLM.
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Office"))
            {
                log.Info("Successfully read reg key: HKLM\\Software\\Microsoft\\Office");
                string[] subkeys = key.GetSubKeyNames();

                foreach (string subkey in subkeys)
                {
                    log.Info("Analyzing subkey '" + subkey + "'");
                    double versionSubKey;
                    CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
                    if (double.TryParse(subkey, System.Globalization.NumberStyles.Float, culture, out versionSubKey))
                    {
                        log.Info("Office Version: " + versionSubKey.ToString());
                        if (versionSubKey > 9)
                        {
                            hasOffice2000OrHigher = true;
                            break;
                        }
                    }
{'fraction_non_alphanumeric': 0.08594331047020907, 'fraction_numerical': 0.009585984629369474, 'mean_word_length': 1.6857523302263648, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4299977', 'n_tokens_mistral': 2952, 'n_tokens_neox': 2701, 'n_words': 779}