33using System . Drawing ;
44using System . Globalization ;
55using System . IO ;
6+ using System . Windows . Forms ;
67using Microsoft . Win32 ;
78using OpenHardwareMonitor . Hardware ;
89
910namespace OpenHardwareMonitor {
1011
1112 public class PersistentSettings : ISettings {
1213
14+ private readonly string configFilePath ;
15+
16+ public PersistentSettings ( ) {
17+ configFilePath = Path . ChangeExtension ( Application . ExecutablePath , ".config" ) ;
18+ }
19+
1320 private IDictionary < string , string > settings = new Dictionary < string , string > ( ) ;
1421
15- public void Load ( string fileName ) {
22+ public void Load ( ) {
1623
1724 //old versions configs compatibility
18- if ( File . Exists ( fileName ) ) {
25+ if ( File . Exists ( configFilePath ) ) {
1926 try {
20- var json = File . ReadAllText ( fileName ) ;
27+ var json = File . ReadAllText ( configFilePath ) ;
2128 settings = json . FromJson < IDictionary < string , string > > ( ) ;
2229 return ;
2330 }
@@ -35,7 +42,7 @@ public void Load(string fileName) {
3542 }
3643 }
3744
38- public void Save ( string fileName ) {
45+ public void Save ( ) {
3946
4047 try {
4148 //remove prev settings
@@ -48,16 +55,30 @@ public void Save(string fileName) {
4855 }
4956 }
5057
51- if ( File . Exists ( fileName ) ) {
58+ if ( File . Exists ( configFilePath ) ) {
5259 try {
53- File . Delete ( fileName ) ;
60+ File . Delete ( configFilePath ) ;
5461 } catch ( Exception ) {
5562 //ignore
5663 }
5764 }
5865 } catch ( Exception ) {
5966 //save old-style config
60- settings . ToJsonFile ( fileName ) ;
67+ SaveToFile ( ) ;
68+ }
69+ }
70+
71+ public void SaveToFile ( ) {
72+ try {
73+ settings . ToJsonFile ( configFilePath ) ;
74+ } catch ( UnauthorizedAccessException ) {
75+ MessageBox . Show ( "Access to the path '" + configFilePath + "' is denied. " +
76+ "The current settings could not be saved." ,
77+ "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
78+ } catch ( IOException ) {
79+ MessageBox . Show ( "The path '" + configFilePath + "' is not writeable. " +
80+ "The current settings could not be saved." ,
81+ "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
6182 }
6283 }
6384
@@ -67,6 +88,7 @@ public bool Contains(string name) {
6788
6889 public void SetValue ( string name , string value ) {
6990 settings [ name ] = value ;
91+ Save ( ) ;
7092 }
7193
7294 public string GetValue ( string name , string defaultValue ) {
@@ -75,10 +97,12 @@ public string GetValue(string name, string defaultValue) {
7597
7698 public void Remove ( string name ) {
7799 settings . Remove ( name ) ;
100+ Save ( ) ;
78101 }
79102
80103 public void SetValue ( string name , int value ) {
81104 settings [ name ] = value . ToString ( ) ;
105+ Save ( ) ;
82106 }
83107
84108 public int GetValue ( string name , int defaultValue ) {
@@ -89,6 +113,7 @@ public int GetValue(string name, int defaultValue) {
89113
90114 public void SetValue ( string name , float value ) {
91115 settings [ name ] = value . ToString ( CultureInfo . InvariantCulture ) ;
116+ Save ( ) ;
92117 }
93118
94119 public float GetValue ( string name , float defaultValue ) {
@@ -100,6 +125,7 @@ public float GetValue(string name, float defaultValue) {
100125
101126 public void SetValue ( string name , bool value ) {
102127 settings [ name ] = value ? "true" : "false" ;
128+ Save ( ) ;
103129 }
104130
105131 public bool GetValue ( string name , bool defaultValue ) {
@@ -110,6 +136,7 @@ public bool GetValue(string name, bool defaultValue) {
110136
111137 public void SetValue ( string name , Color color ) {
112138 settings [ name ] = color . ToArgb ( ) . ToString ( "X8" ) ;
139+ Save ( ) ;
113140 }
114141
115142 public Color GetValue ( string name , Color defaultValue ) {
0 commit comments