WritePrivateProfileString() private method

private WritePrivateProfileString ( string Section, string Key, string Value, string FilePath ) : long
Section string
Key string
Value string
FilePath string
return long
Esempio n. 1
0
        public void LoadEntry(SettingsEntry entry, string section, string key)
        {
            try
            {
                if (!m_mutex.WaitOne(2000))
                {
                    Logger.Warn("Failed to acquire settings lock");
                    return;
                }
            }
            catch (AbandonedMutexException)
            {
                // Ignore; this might be a previous instance that crashed
            }

            try
            {
                const int len          = 255;
                var       must_migrate = false;
                var       tmp          = new StringBuilder(len);
                var       result       = NativeMethods.GetPrivateProfileString(section, key, entry.ToString(),
                                                                               tmp, len, FullPath);
                if (result == 0)
                {
                    // Compatibility code for keys that moved from the "global"
                    // to the "composing" or "tweaks" section.
                    if (section != "global")
                    {
                        result = NativeMethods.GetPrivateProfileString("global", key, entry.ToString(),
                                                                       tmp, len, FullPath);
                        if (result == 0)
                        {
                            return;
                        }
                        must_migrate = true;
                    }
                }

                // This may throw, but will be caught gracefully
                entry.LoadString(tmp.ToString());

                if (must_migrate)
                {
                    NativeMethods.WritePrivateProfileString("global", key, null,
                                                            FullPath);
                    NativeMethods.WritePrivateProfileString(section, key, entry.ToString(),
                                                            FullPath);
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex, $"Failed to load settings entry {section}.{key}");
            }
            finally
            {
                // Ensure the mutex is always released even if an
                // exception is thrown
                m_mutex.ReleaseMutex();
            }
        }
Esempio n. 2
0
        private static void SaveEntry(string value, string section, string key)
        {
            if (CreateConfigDir())
            {
                try
                {
                    if (!m_mutex.WaitOne(2000))
                    {
                        return;
                    }
                }
                catch (AbandonedMutexException)
                {
                    /* Ignore; this might be a previous instance that crashed */
                }

                try
                {
                    Log.Debug($"Saving {section}.{key} = {value}");
                    NativeMethods.WritePrivateProfileString(section, key, value,
                                                            GetConfigFile());
                }
                finally
                {
                    // Ensure the mutex is always released even if an
                    // exception is thrown
                    m_mutex.ReleaseMutex();
                }
            }
        }
Esempio n. 3
0
        private static void LoadEntry(SettingsEntry entry, string section, string key)
        {
            try
            {
                if (!m_mutex.WaitOne(2000))
                {
                    return;
                }
            }
            catch (AbandonedMutexException)
            {
                /* Ignore; this might be a previous instance that crashed */
            }

            try
            {
                const int len      = 255;
                var       migrated = false;
                var       tmp      = new StringBuilder(len);
                var       result   = NativeMethods.GetPrivateProfileString(section, key, "",
                                                                           tmp, len, GetConfigFile());
                if (result == 0)
                {
                    // Compatibility code for keys that moved from the "global"
                    // to the "composing" or "tweaks" section.
                    if (section != "global")
                    {
                        result = NativeMethods.GetPrivateProfileString("global", key, "",
                                                                       tmp, len, GetConfigFile());
                        if (result == 0)
                        {
                            return;
                        }
                        migrated = true;
                    }
                }

                entry.LoadString(tmp.ToString());

                if (migrated)
                {
                    NativeMethods.WritePrivateProfileString("global", key, null,
                                                            GetConfigFile());
                    NativeMethods.WritePrivateProfileString(section, key, entry.ToString(),
                                                            GetConfigFile());
                }
            }
            finally
            {
                // Ensure the mutex is always released even if an
                // exception is thrown
                m_mutex.ReleaseMutex();
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Saves this settings entry into the settings file. This operation
 /// is thread-safe and process-safe.
 /// </summary>
 /// <returns>A <see cref="bool"/> indicating whether the operation
 /// was successful.</returns>
 public bool Save()
 {
     if (Settings.CreateConfigDir() && m_mutex.WaitOne(2000))
     {
         try
         {
             var string_value = Serialize(Value);
             var result       = NativeMethods.WritePrivateProfileString(Section, Key, string_value, Settings.GetConfigFile());
             return(result == 0);
         }
         finally
         {
             // Ensure the mutex is always released even if an
             // exception is thrown
             m_mutex.ReleaseMutex();
         }
     }
     return(false);
 }
Esempio n. 5
0
        public void SaveEntry(string value, string section, string key)
        {
            if (!Utils.EnsureDirectory(Path.GetDirectoryName(FullPath)))
            {
                return;
            }

            try
            {
                if (!m_mutex.WaitOne(2000))
                {
                    Log.Debug($"Failed to acquire settings lock");
                    return;
                }
            }
            catch (AbandonedMutexException)
            {
                // Ignore; this might be a previous instance that crashed
            }

            try
            {
                Log.Debug($"Saving {section}.{key} = {value}");
                NativeMethods.WritePrivateProfileString(section, key, value, FullPath);
                // Ensure old keys are removed from the global section
                if (section != "global")
                {
                    NativeMethods.WritePrivateProfileString("global", key, null, FullPath);
                }
            }
            catch (Exception ex)
            {
                Log.Debug($"Failed to save settings: {ex}");
            }
            finally
            {
                // Ensure the mutex is always released even if an
                // exception is thrown
                m_mutex.ReleaseMutex();
            }
        }
Esempio n. 6
0
 private static void SaveEntry(string key, string val)
 {
     NativeMethods.WritePrivateProfileString("global", key, val,
                                             GetConfigFile());
 }