1- using System . Configuration ;
1+ using System . Collections . Generic ;
2+ using System ;
3+ using System . Configuration ;
24using System . IO ;
35using System . Reflection ;
6+ using System . Xml ;
47using Configuration = System . Configuration . Configuration ;
8+ using System . Windows . Input ;
59
610namespace EVerse . Navisworks . SelectByRevitId . Plugin
711{
812 public static class SettingsConfig
913 {
10- private static readonly string BinaryLocation = Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
11- private static string appSettingsName = string . Concat ( Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name , ".dll.config" ) ;
12- private static string appSettingsFile = System . IO . Path . Combine ( BinaryLocation , appSettingsName ) ;
14+ public static readonly string currentVersion = "1.0.20" ;
15+ public static readonly string currentApiKey = "PlaceHolderApiKey" ;
16+
17+ private static readonly string _configDir =
18+ Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , "Pris" ) ;
19+
20+ private static readonly string _configFile =
21+ Path . Combine ( _configDir , "pris.config" ) ;
22+
23+ private static readonly object _locker = new object ( ) ;
24+
25+ static SettingsConfig ( )
26+ {
27+ // Ensure folder exists
28+ if ( ! Directory . Exists ( _configDir ) )
29+ Directory . CreateDirectory ( _configDir ) ;
30+
31+ // Ensure file exists with defaults
32+ if ( ! File . Exists ( _configFile ) )
33+ CreateDefaultConfig ( ) ;
34+ }
1335
1436 public static string GetValue ( string key )
1537 {
16- Configuration configuration = ConfigurationManager . OpenMappedExeConfiguration ( new ExeConfigurationFileMap { ExeConfigFilename = appSettingsFile } , ConfigurationUserLevel . None ) ;
17- return configuration . AppSettings . Settings [ key ] . Value ;
38+ if ( ! File . Exists ( _configFile ) )
39+ CreateDefaultConfig ( ) ;
40+
41+ lock ( _locker )
42+ {
43+ try
44+ {
45+ Configuration config = OpenConfig ( ) ;
46+ KeyValueConfigurationElement setting = config . AppSettings . Settings [ key ] ;
47+ return setting != null ? setting . Value : null ;
48+ }
49+ catch ( Exception ex )
50+ {
51+ throw new InvalidOperationException (
52+ $ "Error retrieving value for key '{ key } '.", ex ) ;
53+ }
54+ }
1855 }
1956
2057 public static void SetValue ( string key , string value )
2158 {
22- Configuration configuration = ConfigurationManager . OpenMappedExeConfiguration ( new ExeConfigurationFileMap { ExeConfigFilename = appSettingsFile } , ConfigurationUserLevel . None ) ;
23- configuration . AppSettings . Settings [ key ] . Value = value ;
24- configuration . Save ( ConfigurationSaveMode . Modified , true ) ;
25- ConfigurationManager . RefreshSection ( "appSettings" ) ;
59+ if ( ! File . Exists ( _configFile ) )
60+ CreateDefaultConfig ( ) ;
61+
62+ lock ( _locker )
63+ {
64+ try
65+ {
66+ Configuration config = OpenConfig ( ) ;
67+ KeyValueConfigurationCollection settings = config . AppSettings . Settings ;
68+
69+ if ( settings [ key ] == null )
70+ settings . Add ( key , value ) ;
71+ else
72+ settings [ key ] . Value = value ;
73+
74+ config . Save ( ConfigurationSaveMode . Modified ) ;
75+ ConfigurationManager . RefreshSection ( "appSettings" ) ;
76+ }
77+ catch ( Exception ex )
78+ {
79+ throw new InvalidOperationException (
80+ $ "Error setting value for key '{ key } '.", ex ) ;
81+ }
82+ }
83+ }
84+ private static Configuration OpenConfig ( )
85+ {
86+ var map = new ExeConfigurationFileMap { ExeConfigFilename = _configFile } ;
87+ return ConfigurationManager . OpenMappedExeConfiguration ( map , ConfigurationUserLevel . None ) ;
88+ }
89+
90+ /// <summary>
91+ /// Creates leia.config with the requested default keys/values.
92+ /// </summary>
93+ private static void CreateDefaultConfig ( )
94+ {
95+ // Your requested defaults (removed the duplicated "runs" key to avoid errors).
96+ var defaults = new Dictionary < string , string >
97+ {
98+ { "runs" , "0" } ,
99+ { "version" , "0.0.0" } ,
100+ { "user" , "user01" } ,
101+ { "release" , "0" } ,
102+ { "apikey" , "0" }
103+ } ;
104+
105+
106+ var doc = new XmlDocument ( ) ;
107+ var decl = doc . CreateXmlDeclaration ( "1.0" , "utf-8" , null ) ;
108+ doc . AppendChild ( decl ) ;
109+
110+ XmlElement configuration = doc . CreateElement ( "configuration" ) ;
111+ doc . AppendChild ( configuration ) ;
112+
113+ XmlElement appSettings = doc . CreateElement ( "appSettings" ) ;
114+ configuration . AppendChild ( appSettings ) ;
115+
116+ foreach ( var kvp in defaults )
117+ {
118+ XmlElement add = doc . CreateElement ( "add" ) ;
119+ add . SetAttribute ( "key" , kvp . Key ) ;
120+ add . SetAttribute ( "value" , kvp . Value ) ;
121+ appSettings . AppendChild ( add ) ;
122+ }
123+
124+ doc . Save ( _configFile ) ;
26125 }
27126 }
28- }
127+ }
0 commit comments