public void SaveSettings()
        {
            _iniConfigFileManager.ConfigurationData.ConfigItems.Clear();
            _iniConfigFileManager.ConfigurationData.ConfigSections.Clear();

            // Exchange rates
            IniConfigFileSection configSection = new IniConfigFileSection();

            configSection.ConfigItems.Add("BTC_Price", _settings.BTC_Price.ToString());
            configSection.ConfigItems.Add("SEK_USD_Rate", _settings.SEK_USD_Rate.ToString());
            configSection.ConfigItems.Add("Commision", _settings.Commision.ToString());
            _iniConfigFileManager.ConfigurationData.ConfigSections.Add("ExchangeRates", configSection);

            // General settings
            configSection = new IniConfigFileSection();
            configSection.ConfigItems.Add("Topmost", _settings.Topmost ? "1" : "0");
            configSection.ConfigItems.Add("AutofocusPrice", _settings.AutofocusPrice ? "1" : "0");
            if (_settings.StartPosition != null)
            {
                configSection.ConfigItems.Add("StartupPosX", _settings.StartPosition.X.ToString());
                configSection.ConfigItems.Add("StartupPosY", _settings.StartPosition.Y.ToString());
            }

            _iniConfigFileManager.ConfigurationData.ConfigSections.Add("UserInterface", configSection);
            _iniConfigFileManager.SaveConfigFile(_appSettingsFilename);
        }
        public void SaveConfigFile(string path)
        {
            try
            {
                if (!Utils.GetFileNameFromPath(path).EndsWith(".ini"))
                {
                    throw new ArgumentException("Invalid filename. Filename must end with .ini");
                }

                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                FileStream fs = File.Create(path);
                TextWriter tw = new StreamWriter(fs);

                foreach (string rootItemName in _iniFileData.ConfigItems.AllKeys)
                {
                    tw.WriteLine(rootItemName + "=" + _iniFileData.ConfigItems[rootItemName]);
                }

                foreach (string sectionName in _iniFileData.ConfigSections.Keys)
                {
                    IniConfigFileSection section = _iniFileData.ConfigSections[sectionName];
                    tw.WriteLine("[" + sectionName + "]");

                    foreach (string itemName in section.ConfigItems.AllKeys)
                    {
                        tw.WriteLine(itemName + "=" + section.ConfigItems[itemName]);
                    }
                }
                tw.Flush();
                fs.Flush();

                fs.Close();
            }
            catch (Exception ex)
            {
                LogWriter.WriteLog(ex.Message);
            }
        }
        public bool LoadSettings()
        {
            try
            {
                if (!File.Exists(_appSettingsFilename))
                {
                    return(false);
                }

                if (_iniConfigFileManager.LoadConfigFile(_appSettingsFilename))
                {
                    _settings = new PriceCalcSettings();

                    IniConfigFileSection exchangeRateConfigFileSection = _iniConfigFileManager.ConfigurationData.ConfigSections["ExchangeRates"];
                    _settings.BTC_Price    = double.Parse(exchangeRateConfigFileSection.ConfigItems["BTC_Price"]);
                    _settings.SEK_USD_Rate = double.Parse(exchangeRateConfigFileSection.ConfigItems["SEK_USD_Rate"]);
                    _settings.Commision    = double.Parse(exchangeRateConfigFileSection.ConfigItems["Commision"]);

                    IniConfigFileSection uiConfigFileSection = _iniConfigFileManager.ConfigurationData.ConfigSections["UserInterface"];
                    _settings.Topmost        = uiConfigFileSection.ConfigItems["Topmost"] == "1";
                    _settings.AutofocusPrice = uiConfigFileSection.ConfigItems["AutofocusPrice"] == "1";

                    if (uiConfigFileSection.ConfigItems["StartupPosX"] != null)
                    {
                        int startX = int.Parse(uiConfigFileSection.ConfigItems["StartupPosX"]);
                        int startY = int.Parse(uiConfigFileSection.ConfigItems["StartupPosY"]);
                        _settings.StartPosition = new Coordinate(startX, startY);
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
            }
            return(false);
        }
        public bool LoadConfigFile(string path)
        {
            bool       readSuccessfull = false;
            FileStream fs = null;

            try
            {
                if (!Utils.GetFileNameFromPath(path).EndsWith(".ini") || !File.Exists(path))
                {
                    throw new ArgumentException("File does not exist!");
                }

                fs = File.OpenRead(path);
                TextReader tr = new StreamReader(fs);

                Regex sectionMachRegex    = new Regex(ConfigFileSectionPattern);
                Regex sectionCleanerRegex = new Regex(ConfigSectionNamePattern);
                Regex configItemPattern   = new Regex(ConfigFileItemPattern);

                _iniFileData = new IniConfigFile();
                IniConfigFileSection configSection = new IniConfigFileSection();


                while (fs.Position <= fs.Length)
                {
                    string lineData = tr.ReadLine();

                    if (lineData == null || lineData.Length > 4096)
                    {
                        break;
                    }

                    if (sectionMachRegex.IsMatch(lineData))
                    {
                        string sectionName = sectionCleanerRegex.Replace(lineData, "");
                        if (!_iniFileData.ConfigSections.ContainsKey(sectionName))
                        {
                            _iniFileData.ConfigSections.Add(sectionName, new IniConfigFileSection());
                        }

                        configSection = _iniFileData.ConfigSections[sectionName];
                    }
                    else if (configSection != null && configItemPattern.IsMatch(lineData))
                    {
                        string[] confArr = lineData.Split('=');
                        configSection.ConfigItems[confArr[0].Trim()] = confArr[1].Trim();
                    }
                }
                fs.Close();
                readSuccessfull = true;
            }
            catch (Exception ex)
            {
                LogWriter.WriteLog(ex.Message);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }

            return(readSuccessfull);
        }