99namespace OpenHardwareMonitor {
1010 public class PersistentSettings : ISettings {
1111
12- private IDictionary < string , string > settings =
12+ private IDictionary < string , string > settings =
1313 new Dictionary < string , string > ( ) ;
1414
1515 public void Load ( string fileName ) {
@@ -36,14 +36,14 @@ public void Load(string fileName) {
3636 XmlNodeList list = doc . GetElementsByTagName ( "appSettings" ) ;
3737 foreach ( XmlNode node in list ) {
3838 XmlNode parent = node . ParentNode ;
39- if ( parent != null && parent . Name == "configuration" &&
39+ if ( parent != null && parent . Name == "configuration" &&
4040 parent . ParentNode is XmlDocument ) {
4141 foreach ( XmlNode child in node . ChildNodes ) {
4242 if ( child . Name == "add" ) {
4343 XmlAttributeCollection attributes = child . Attributes ;
4444 XmlAttribute keyAttribute = attributes [ "key" ] ;
4545 XmlAttribute valueAttribute = attributes [ "value" ] ;
46- if ( keyAttribute != null && valueAttribute != null &&
46+ if ( keyAttribute != null && valueAttribute != null &&
4747 keyAttribute . Value != null ) {
4848 settings . Add ( keyAttribute . Value , valueAttribute . Value ) ;
4949 }
@@ -86,7 +86,7 @@ public void Save(string fileName) {
8686 } catch { }
8787 }
8888
89- using ( var stream = new FileStream ( fileName ,
89+ using ( var stream = new FileStream ( fileName ,
9090 FileMode . Create , FileAccess . Write ) )
9191 {
9292 stream . Write ( file , 0 , file . Length ) ;
@@ -105,12 +105,8 @@ public void SetValue(string name, string value) {
105105 settings [ name ] = value ;
106106 }
107107
108- public string GetValue ( string name , string value ) {
109- string result ;
110- if ( settings . TryGetValue ( name , out result ) )
111- return result ;
112- else
113- return value ;
108+ public string GetValue ( string name , string defaultValue ) {
109+ return settings . TryGetValue ( name , out var result ) ? result : defaultValue ;
114110 }
115111
116112 public void Remove ( string name ) {
@@ -121,66 +117,42 @@ public void SetValue(string name, int value) {
121117 settings [ name ] = value . ToString ( ) ;
122118 }
123119
124- public int GetValue ( string name , int value ) {
125- string str ;
126- if ( settings . TryGetValue ( name , out str ) ) {
127- int parsedValue ;
128- if ( int . TryParse ( str , out parsedValue ) )
129- return parsedValue ;
130- else
131- return value ;
132- } else {
133- return value ;
134- }
120+ public int GetValue ( string name , int defaultValue ) {
121+ if ( settings . TryGetValue ( name , out var str ) && int . TryParse ( str , out var parsedValue ) )
122+ return parsedValue ;
123+ return defaultValue ;
135124 }
136125
137126 public void SetValue ( string name , float value ) {
138127 settings [ name ] = value . ToString ( CultureInfo . InvariantCulture ) ;
139128 }
140129
141- public float GetValue ( string name , float value ) {
142- string str ;
143- if ( settings . TryGetValue ( name , out str ) ) {
144- float parsedValue ;
145- if ( float . TryParse ( str , NumberStyles . Float ,
146- CultureInfo . InvariantCulture , out parsedValue ) )
147- return parsedValue ;
148- else
149- return value ;
150- } else {
151- return value ;
152- }
130+ public float GetValue ( string name , float defaultValue ) {
131+ if ( settings . TryGetValue ( name , out var str ) && float . TryParse ( str , NumberStyles . Float ,
132+ CultureInfo . InvariantCulture , out var parsedValue ) )
133+ return parsedValue ;
134+ return defaultValue ;
153135 }
154136
155137 public void SetValue ( string name , bool value ) {
156138 settings [ name ] = value ? "true" : "false" ;
157139 }
158140
159- public bool GetValue ( string name , bool value ) {
160- string str ;
161- if ( settings . TryGetValue ( name , out str ) ) {
141+ public bool GetValue ( string name , bool defaultValue ) {
142+ if ( settings . TryGetValue ( name , out var str ) )
162143 return str == "true" ;
163- } else {
164- return value ;
165- }
144+ return defaultValue ;
166145 }
167146
168147 public void SetValue ( string name , Color color ) {
169148 settings [ name ] = color . ToArgb ( ) . ToString ( "X8" ) ;
170149 }
171150
172- public Color GetValue ( string name , Color value ) {
173- string str ;
174- if ( settings . TryGetValue ( name , out str ) ) {
175- int parsedValue ;
176- if ( int . TryParse ( str , NumberStyles . HexNumber ,
177- CultureInfo . InvariantCulture , out parsedValue ) )
178- return Color . FromArgb ( parsedValue ) ;
179- else
180- return value ;
181- } else {
182- return value ;
183- }
151+ public Color GetValue ( string name , Color defaultValue ) {
152+ if ( settings . TryGetValue ( name , out var str ) && int . TryParse ( str , NumberStyles . HexNumber ,
153+ CultureInfo . InvariantCulture , out var parsedValue ) )
154+ return Color . FromArgb ( parsedValue ) ;
155+ return defaultValue ;
184156 }
185157 }
186158}
0 commit comments