CustomizableFileSettingsProvider.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. //
  2. // CustomizableLocalFileSettingsProvider.cs
  3. //
  4. // Authors:
  5. // Noriaki Okimoto <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // (C)2007 Noriaki Okimoto
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0 && CONFIGURATION_DEP
  30. #if !TARGET_JVM
  31. extern alias PrebuiltSystem;
  32. #endif
  33. using System;
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using System.Configuration;
  37. using System.IO;
  38. using System.Reflection;
  39. using System.Security.Cryptography;
  40. using System.Text;
  41. using System.Xml;
  42. #if TARGET_JVM
  43. using NameValueCollection = System.Collections.Specialized.NameValueCollection;
  44. #else
  45. using NameValueCollection = PrebuiltSystem.System.Collections.Specialized.NameValueCollection;
  46. #endif
  47. namespace System.Configuration
  48. {
  49. // location to store user configuration settings.
  50. internal enum UserConfigLocationOption : uint
  51. {
  52. Product = 0x20,
  53. Product_VersionMajor = 0x21,
  54. Product_VersionMinor = 0x22,
  55. Product_VersionBuild = 0x24,
  56. Product_VersionRevision = 0x28,
  57. Company_Product = 0x30,
  58. Company_Product_VersionMajor = 0x31,
  59. Company_Product_VersionMinor = 0x32,
  60. Company_Product_VersionBuild = 0x34,
  61. Company_Product_VersionRevision = 0x38,
  62. Other = 0x8000
  63. }
  64. internal class CustomizableFileSettingsProvider : SettingsProvider, IApplicationSettingsProvider
  65. {
  66. private static string userRoamingPath = "";
  67. private static string userLocalPath = "";
  68. private static string userRoamingPathPrevVersion = "";
  69. private static string userLocalPathPrevVersion = "";
  70. private static string userRoamingName = "user.config";
  71. private static string userLocalName = "user.config";
  72. private static string userRoamingBasePath = "";
  73. private static string userLocalBasePath = "";
  74. private static string CompanyName = "";
  75. private static string ProductName = "";
  76. private static string ForceVersion = "";
  77. private static string[] ProductVersion;
  78. // whether to include parts in the folder name or not:
  79. private static bool isVersionMajor = false; // 0x0001 major version
  80. private static bool isVersionMinor = false; // 0x0002 minor version
  81. private static bool isVersionBuild = false; // 0x0004 build version
  82. private static bool isVersionRevision = false; // 0x0008 revision
  83. private static bool isCompany = true; // 0x0010 corporate name
  84. private static bool isProduct = true; // 0x0020 product name
  85. private static bool userDefine = false; // 0x8000 ignore all above and use user definition
  86. private static UserConfigLocationOption userConfig = UserConfigLocationOption.Company_Product;
  87. public override void Initialize (string name, NameValueCollection config)
  88. {
  89. base.Initialize (name, config);
  90. }
  91. // full path to roaming user.config
  92. internal static string UserRoamingFullPath {
  93. get { return Path.Combine (userRoamingPath, userRoamingName); }
  94. }
  95. // full path to local user.config
  96. internal static string UserLocalFullPath {
  97. get { return Path.Combine (userLocalPath, userLocalName); }
  98. }
  99. // previous full path to roaming user.config
  100. public static string PrevUserRoamingFullPath {
  101. get { return Path.Combine (userRoamingPathPrevVersion, userRoamingName); }
  102. }
  103. // previous full path to local user.config
  104. public static string PrevUserLocalFullPath {
  105. get { return Path.Combine (userLocalPathPrevVersion, userLocalName); }
  106. }
  107. // path to roaming user.config
  108. public static string UserRoamingPath {
  109. get { return userRoamingPath; }
  110. }
  111. // path to local user.config
  112. public static string UserLocalPath {
  113. get { return userLocalPath; }
  114. }
  115. // file name which is equivalent to user.config, for roaming user
  116. public static string UserRoamingName {
  117. get { return userRoamingName; }
  118. }
  119. // file name which is equivalent to user.config, for local user
  120. public static string UserLocalName {
  121. get { return userLocalName; }
  122. }
  123. public static UserConfigLocationOption UserConfigSelector
  124. {
  125. get { return userConfig; }
  126. set {
  127. userConfig = value;
  128. if (((uint) userConfig & 0x8000) != 0) {
  129. isVersionMajor = false;
  130. isVersionMinor = false;
  131. isVersionBuild = false;
  132. isVersionRevision = false;
  133. isCompany = false;
  134. return;
  135. }
  136. isVersionRevision = ((uint) userConfig & 0x0008) != 0;
  137. isVersionBuild = isVersionRevision | ((uint)userConfig & 0x0004) != 0;
  138. isVersionMinor = isVersionBuild | ((uint)userConfig & 0x0002) != 0;
  139. isVersionMajor = IsVersionMinor | ((uint)userConfig & 0x0001) != 0;
  140. isCompany = ((uint) userConfig & 0x0010) != 0;
  141. isProduct = ((uint) userConfig & 0x0020) != 0;
  142. }
  143. }
  144. // whether the path to include the major version.
  145. public static bool IsVersionMajor
  146. {
  147. get { return isVersionMajor; }
  148. set
  149. {
  150. isVersionMajor = value;
  151. isVersionMinor = false;
  152. isVersionBuild = false;
  153. isVersionRevision = false;
  154. }
  155. }
  156. // whether the path to include minor version.
  157. public static bool IsVersionMinor
  158. {
  159. get { return isVersionMinor; }
  160. set
  161. {
  162. isVersionMinor = value;
  163. if (isVersionMinor)
  164. isVersionMajor = true;
  165. isVersionBuild = false;
  166. isVersionRevision = false;
  167. }
  168. }
  169. // whether the path to include build version.
  170. public static bool IsVersionBuild
  171. {
  172. get { return isVersionBuild; }
  173. set
  174. {
  175. isVersionBuild = value;
  176. if (isVersionBuild) {
  177. isVersionMajor = true;
  178. isVersionMinor = true;
  179. }
  180. isVersionRevision = false;
  181. }
  182. }
  183. // whether the path to include revision.
  184. public static bool IsVersionRevision
  185. {
  186. get { return isVersionRevision; }
  187. set
  188. {
  189. isVersionRevision = value;
  190. if (isVersionRevision) {
  191. isVersionMajor = true;
  192. isVersionMinor = true;
  193. isVersionBuild = true;
  194. }
  195. }
  196. }
  197. // whether the path to include company name.
  198. public static bool IsCompany
  199. {
  200. get { return isCompany; }
  201. set { isCompany = value; }
  202. }
  203. // AssemblyCompanyAttribute->Namespace->"Program"
  204. private static string GetCompanyName ()
  205. {
  206. Assembly assembly = Assembly.GetEntryAssembly ();
  207. if (assembly == null)
  208. assembly = Assembly.GetCallingAssembly ();
  209. AssemblyCompanyAttribute [] attrs = (AssemblyCompanyAttribute []) assembly.GetCustomAttributes (typeof (AssemblyCompanyAttribute), true);
  210. if ((attrs != null) && attrs.Length > 0) {
  211. return attrs [0].Company;
  212. }
  213. #if !TARGET_JVM
  214. MethodInfo entryPoint = assembly.EntryPoint;
  215. Type entryType = entryPoint != null ? entryPoint.DeclaringType : null;
  216. if (entryType != null && !String.IsNullOrEmpty (entryType.Namespace)) {
  217. int end = entryType.Namespace.IndexOf ('.');
  218. return end < 0 ? entryType.Namespace : entryType.Namespace.Substring (0, end);
  219. }
  220. return "Program";
  221. #else
  222. return assembly.GetName ().Name;
  223. #endif
  224. }
  225. private static string GetProductName ()
  226. {
  227. Assembly assembly = Assembly.GetEntryAssembly ();
  228. if (assembly == null)
  229. assembly = Assembly.GetCallingAssembly ();
  230. #if !TARGET_JVM
  231. byte [] pkt = assembly.GetName ().GetPublicKeyToken ();
  232. byte [] hash = SHA1.Create ().ComputeHash (pkt != null ? pkt : Encoding.UTF8.GetBytes (assembly.EscapedCodeBase));
  233. return String.Format ("{0}_{1}_{2}",
  234. AppDomain.CurrentDomain.FriendlyName,
  235. pkt != null ? "StrongName" : "Url",
  236. // FIXME: it seems that something else is used
  237. // here, to convert hash bytes to string.
  238. Convert.ToBase64String (hash));
  239. #else // AssemblyProductAttribute-based code
  240. AssemblyProductAttribute [] attrs = (AssemblyProductAttribute[]) assembly.GetCustomAttributes (typeof (AssemblyProductAttribute), true);
  241. if ((attrs != null) && attrs.Length > 0) {
  242. return attrs [0].Product;
  243. }
  244. return assembly.GetName ().Name;
  245. #endif
  246. }
  247. private static string GetProductVersion ()
  248. {
  249. Assembly assembly = Assembly.GetEntryAssembly ();
  250. if (assembly == null)
  251. assembly = Assembly.GetCallingAssembly ();
  252. if (assembly == null)
  253. return string.Empty;
  254. return assembly.GetName ().Version.ToString ();
  255. }
  256. private static void CreateUserConfigPath ()
  257. {
  258. if (userDefine)
  259. return;
  260. if (ProductName == "")
  261. ProductName = GetProductName ();
  262. if (CompanyName == "")
  263. CompanyName = GetCompanyName ();
  264. if (ForceVersion == "")
  265. ProductVersion = GetProductVersion ().Split('.');
  266. // C:\Documents and Settings\(user)\Application Data
  267. #if !TARGET_JVM
  268. if (userRoamingBasePath == "")
  269. userRoamingPath = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
  270. else
  271. #endif
  272. userRoamingPath = userRoamingBasePath;
  273. // C:\Documents and Settings\(user)\Local Settings\Application Data (on Windows)
  274. #if !TARGET_JVM
  275. if (userLocalBasePath == "")
  276. userLocalPath = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData);
  277. else
  278. #endif
  279. userLocalPath = userLocalBasePath;
  280. if (isCompany) {
  281. userRoamingPath = Path.Combine (userRoamingPath, CompanyName);
  282. userLocalPath = Path.Combine (userLocalPath, CompanyName);
  283. }
  284. if (isProduct) {
  285. userRoamingPath = Path.Combine (userRoamingPath, ProductName);
  286. userLocalPath = Path.Combine (userLocalPath, ProductName);
  287. }
  288. string versionName;
  289. if (ForceVersion == "") {
  290. if (isVersionRevision)
  291. versionName = String.Format ("{0}.{1}.{2}.{3}", ProductVersion [0], ProductVersion [1], ProductVersion [2], ProductVersion [3]);
  292. else if (isVersionBuild)
  293. versionName = String.Format ("{0}.{1}.{2}", ProductVersion [0], ProductVersion [1], ProductVersion [2]);
  294. else if (isVersionMinor)
  295. versionName = String.Format ("{0}.{1}", ProductVersion [0], ProductVersion [1]);
  296. else if (isVersionMajor)
  297. versionName = ProductVersion [0];
  298. else
  299. versionName = "";
  300. }
  301. else
  302. versionName = ForceVersion;
  303. string prevVersionRoaming = PrevVersionPath (userRoamingPath, versionName);
  304. string prevVersionLocal = PrevVersionPath (userLocalPath, versionName);
  305. userRoamingPath = Path.Combine (userRoamingPath, versionName);
  306. userLocalPath = Path.Combine (userLocalPath, versionName);
  307. if (prevVersionRoaming != "")
  308. userRoamingPathPrevVersion = Path.Combine(userRoamingPath, prevVersionRoaming);
  309. if (prevVersionLocal != "")
  310. userLocalPathPrevVersion = Path.Combine(userLocalPath, prevVersionLocal);
  311. }
  312. // string for the previous version. It ignores newer ones.
  313. private static string PrevVersionPath (string dirName, string currentVersion)
  314. {
  315. string prevVersionString = "";
  316. if (!Directory.Exists(dirName))
  317. return prevVersionString;
  318. DirectoryInfo currentDir = new DirectoryInfo (dirName);
  319. foreach (DirectoryInfo dirInfo in currentDir.GetDirectories ())
  320. if (String.Compare (currentVersion, dirInfo.Name, StringComparison.Ordinal) > 0)
  321. if (String.Compare (prevVersionString, dirInfo.Name, StringComparison.Ordinal) < 0)
  322. prevVersionString = dirInfo.Name;
  323. return prevVersionString;
  324. }
  325. // sets the explicit path to store roaming user.config or equivalent.
  326. // (returns the path validity.)
  327. public static bool SetUserRoamingPath (string configPath)
  328. {
  329. if (CheckPath (configPath))
  330. {
  331. userRoamingBasePath = configPath;
  332. return true;
  333. }
  334. else
  335. return false;
  336. }
  337. // sets the explicit path to store local user.config or equivalent.
  338. // (returns the path validity.)
  339. public static bool SetUserLocalPath (string configPath)
  340. {
  341. if (CheckPath (configPath))
  342. {
  343. userLocalBasePath = configPath;
  344. return true;
  345. }
  346. else
  347. return false;
  348. }
  349. private static bool CheckFileName (string configFile)
  350. {
  351. /*
  352. char[] invalidFileChars = Path.GetInvalidFileNameChars();
  353. foreach (char invalidChar in invalidFileChars)
  354. {
  355. if (configFile.Contains(invalidChar.ToString()))
  356. {
  357. return false;
  358. }
  359. }
  360. return true;
  361. */
  362. return configFile.IndexOfAny (Path.GetInvalidFileNameChars ()) < 0;
  363. }
  364. // sets the explicit roaming file name which is user.config equivalent.
  365. // (returns the file name validity.)
  366. public static bool SetUserRoamingFileName (string configFile)
  367. {
  368. if (CheckFileName (configFile))
  369. {
  370. userRoamingName = configFile;
  371. return true;
  372. }
  373. else
  374. return false;
  375. }
  376. // sets the explicit local file name which is user.config equivalent.
  377. // (returns the file name validity.)
  378. public static bool SetUserLocalFileName (string configFile)
  379. {
  380. if (CheckFileName (configFile))
  381. {
  382. userLocalName = configFile;
  383. return true;
  384. }
  385. else
  386. return false;
  387. }
  388. // sets the explicit company name for folder.
  389. // (returns the file name validity.)
  390. public static bool SetCompanyName (string companyName)
  391. {
  392. if (CheckFileName (companyName))
  393. {
  394. CompanyName = companyName;
  395. return true;
  396. }
  397. else
  398. return false;
  399. }
  400. // sets the explicit product name for folder.
  401. // (returns the file name validity.)
  402. public static bool SetProductName (string productName)
  403. {
  404. if (CheckFileName (productName))
  405. {
  406. ProductName = productName;
  407. return true;
  408. }
  409. else
  410. return false;
  411. }
  412. // sets the explicit major version for folder.
  413. public static bool SetVersion (int major)
  414. {
  415. ForceVersion = string.Format ("{0}", major);
  416. return true;
  417. }
  418. // sets the explicit major and minor versions for folder.
  419. public static bool SetVersion (int major, int minor)
  420. {
  421. ForceVersion = string.Format ("{0}.{1}", major, minor);
  422. return true;
  423. }
  424. // sets the explicit major/minor/build numbers for folder.
  425. public static bool SetVersion (int major, int minor, int build)
  426. {
  427. ForceVersion = string.Format ("{0}.{1}.{2}", major, minor, build);
  428. return true;
  429. }
  430. // sets the explicit major/minor/build/revision numbers for folder.
  431. public static bool SetVersion (int major, int minor, int build, int revision)
  432. {
  433. ForceVersion = string.Format ("{0}.{1}.{2}.{3}", major, minor, build, revision);
  434. return true;
  435. }
  436. // sets the explicit version number string for folder.
  437. public static bool SetVersion (string forceVersion)
  438. {
  439. if (CheckFileName (forceVersion))
  440. {
  441. ForceVersion = forceVersion;
  442. return true;
  443. }
  444. else
  445. return false;
  446. }
  447. private static bool CheckPath (string configPath)
  448. {
  449. char[] invalidPathChars = Path.GetInvalidPathChars ();
  450. /*
  451. foreach (char invalidChar in invalidPathChars)
  452. {
  453. if (configPath.Contains (invalidChar.ToString()))
  454. {
  455. return false;
  456. }
  457. }
  458. */
  459. if (configPath.IndexOfAny (invalidPathChars) >= 0)
  460. return false;
  461. string folder = configPath;
  462. string fileName;
  463. while ((fileName = Path.GetFileName (folder)) != "")
  464. {
  465. if (!CheckFileName (fileName))
  466. {
  467. return false;
  468. }
  469. folder = Path.GetDirectoryName (folder);
  470. }
  471. return true;
  472. }
  473. public override string Name {
  474. get { return base.Name; }
  475. }
  476. string app_name = String.Empty;//"OJK.CustomSetting.CustomizableLocalFileSettingsProvider";
  477. public override string ApplicationName {
  478. get { return app_name; }
  479. set { app_name = value; }
  480. }
  481. private ExeConfigurationFileMap exeMapCurrent = null;
  482. private ExeConfigurationFileMap exeMapPrev = null;
  483. private SettingsPropertyValueCollection values = null;
  484. private void SaveProperties (ExeConfigurationFileMap exeMap, SettingsPropertyValueCollection collection, ConfigurationUserLevel level, SettingsContext context, bool checkUserLevel)
  485. {
  486. Configuration config = ConfigurationManager.OpenMappedExeConfiguration (exeMap, level);
  487. UserSettingsGroup userGroup = config.GetSectionGroup ("userSettings") as UserSettingsGroup;
  488. bool isRoaming = (level == ConfigurationUserLevel.PerUserRoaming);
  489. #if true // my reimplementation
  490. if (userGroup == null) {
  491. userGroup = new UserSettingsGroup ();
  492. config.SectionGroups.Add ("userSettings", userGroup);
  493. ApplicationSettingsBase asb = context.CurrentSettings;
  494. ClientSettingsSection cs = new ClientSettingsSection ();
  495. userGroup.Sections.Add (asb.GetType ().FullName, cs);
  496. }
  497. bool hasChanges = false;
  498. foreach (ConfigurationSection section in userGroup.Sections) {
  499. ClientSettingsSection userSection = section as ClientSettingsSection;
  500. if (userSection == null)
  501. continue;
  502. foreach (SettingsPropertyValue value in collection) {
  503. if (checkUserLevel && value.Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
  504. continue;
  505. hasChanges = true;
  506. SettingElement element = userSection.Settings.Get (value.Name);
  507. if (element == null) {
  508. element = new SettingElement (value.Name, value.Property.SerializeAs);
  509. userSection.Settings.Add (element);
  510. }
  511. if (element.Value.ValueXml == null)
  512. element.Value.ValueXml = new XmlDocument ().CreateDocumentFragment ();
  513. switch (value.Property.SerializeAs) {
  514. case SettingsSerializeAs.Xml:
  515. element.Value.ValueXml.InnerXml = value.SerializedValue as string;
  516. break;
  517. case SettingsSerializeAs.String:
  518. element.Value.ValueXml.InnerText = value.SerializedValue as string;
  519. break;
  520. case SettingsSerializeAs.Binary:
  521. element.Value.ValueXml.InnerText = Convert.ToBase64String (value.SerializedValue as byte []);
  522. break;
  523. default:
  524. throw new NotImplementedException ();
  525. }
  526. }
  527. }
  528. if (hasChanges)
  529. config.Save (ConfigurationSaveMode.Minimal, true);
  530. #else // original impl. - likely buggy to miss some properties to save
  531. foreach (ConfigurationSection configSection in userGroup.Sections)
  532. {
  533. ClientSettingsSection userSection = configSection as ClientSettingsSection;
  534. if (userSection != null)
  535. {
  536. /*
  537. userSection.Settings.Clear();
  538. foreach (SettingsPropertyValue propertyValue in collection)
  539. {
  540. if (propertyValue.IsDirty)
  541. {
  542. SettingElement element = new SettingElement(propertyValue.Name, SettingsSerializeAs.String);
  543. element.Value.ValueXml = new XmlDocument();
  544. element.Value.ValueXml.InnerXml = (string)propertyValue.SerializedValue;
  545. userSection.Settings.Add(element);
  546. }
  547. }
  548. */
  549. foreach (SettingElement element in userSection.Settings)
  550. {
  551. if (collection [element.Name] != null) {
  552. if (collection [element.Name].Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
  553. continue;
  554. element.SerializeAs = SettingsSerializeAs.String;
  555. element.Value.ValueXml.InnerXml = (string) collection [element.Name].SerializedValue; ///Value = XmlElement
  556. }
  557. }
  558. }
  559. }
  560. config.Save (ConfigurationSaveMode.Minimal, true);
  561. #endif
  562. }
  563. private void LoadPropertyValue (SettingsPropertyCollection collection, SettingElement element, bool allowOverwrite)
  564. {
  565. SettingsProperty prop = collection [element.Name];
  566. if (prop == null) { // see bug #343459
  567. prop = new SettingsProperty (element.Name);
  568. collection.Add (prop);
  569. }
  570. SettingsPropertyValue value = new SettingsPropertyValue (prop);
  571. value.IsDirty = false;
  572. value.SerializedValue = element.Value.ValueXml != null ? element.Value.ValueXml.InnerText : prop.DefaultValue;
  573. try
  574. {
  575. if (allowOverwrite)
  576. values.Remove (element.Name);
  577. values.Add (value);
  578. } catch (ArgumentException) {
  579. throw new ConfigurationErrorsException ();
  580. }
  581. }
  582. private void LoadProperies (ExeConfigurationFileMap exeMap, SettingsPropertyCollection collection, ConfigurationUserLevel level, string sectionGroupName, bool allowOverwrite)
  583. {
  584. Configuration config = ConfigurationManager.OpenMappedExeConfiguration (exeMap,level);
  585. ConfigurationSectionGroup sectionGroup = config.GetSectionGroup (sectionGroupName);
  586. if (sectionGroup != null) {
  587. foreach (ConfigurationSection configSection in sectionGroup.Sections) {
  588. ClientSettingsSection clientSection = configSection as ClientSettingsSection;
  589. if (clientSection != null)
  590. foreach (SettingElement element in clientSection.Settings)
  591. LoadPropertyValue(collection, element, allowOverwrite);
  592. }
  593. }
  594. }
  595. public override void SetPropertyValues (SettingsContext context, SettingsPropertyValueCollection collection)
  596. {
  597. CreateExeMap ();
  598. if (UserLocalFullPath == UserRoamingFullPath)
  599. {
  600. SaveProperties (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoaming, context, false);
  601. } else {
  602. SaveProperties (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoaming, context, true);
  603. SaveProperties (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoamingAndLocal, context, true);
  604. }
  605. }
  606. public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection collection)
  607. {
  608. CreateExeMap ();
  609. if (values == null) {
  610. values = new SettingsPropertyValueCollection ();
  611. LoadProperies (exeMapCurrent, collection, ConfigurationUserLevel.None, "applicationSettings", false);
  612. LoadProperies (exeMapCurrent, collection, ConfigurationUserLevel.None, "userSettings", false);
  613. LoadProperies (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoaming, "userSettings", true);
  614. LoadProperies (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoamingAndLocal, "userSettings", true);
  615. // create default values if not exist
  616. foreach (SettingsProperty p in collection)
  617. if (values [p.Name] == null)
  618. values.Add (new SettingsPropertyValue (p));
  619. }
  620. return values;
  621. }
  622. /// creates an ExeConfigurationFileMap
  623. private void CreateExeMap ()
  624. {
  625. if (exeMapCurrent == null) {
  626. CreateUserConfigPath ();
  627. // current version
  628. exeMapCurrent = new ExeConfigurationFileMap ();
  629. // exeMapCurrent.ExeConfigFilename = System.Windows.Forms.Application.ExecutablePath + ".config";
  630. Assembly entry = Assembly.GetEntryAssembly () ?? Assembly.GetExecutingAssembly ();
  631. exeMapCurrent.ExeConfigFilename = entry.Location + ".config";
  632. exeMapCurrent.LocalUserConfigFilename = UserLocalFullPath;
  633. exeMapCurrent.RoamingUserConfigFilename = UserRoamingFullPath;
  634. // previous version
  635. if ((PrevUserLocalFullPath != "") && (PrevUserRoamingFullPath != ""))
  636. {
  637. exeMapPrev = new ExeConfigurationFileMap();
  638. // exeMapPrev.ExeConfigFilename = System.Windows.Forms.Application.ExecutablePath + ".config";
  639. exeMapPrev.ExeConfigFilename = entry.Location + ".config";
  640. exeMapPrev.LocalUserConfigFilename = PrevUserLocalFullPath;
  641. exeMapPrev.RoamingUserConfigFilename = PrevUserRoamingFullPath;
  642. }
  643. }
  644. }
  645. // FIXME: implement
  646. public SettingsPropertyValue GetPreviousVersion (SettingsContext context, SettingsProperty property)
  647. {
  648. return null;
  649. }
  650. public void Reset (SettingsContext context)
  651. {
  652. CreateExeMap ();
  653. foreach (SettingsPropertyValue propertyValue in values) {
  654. propertyValue.PropertyValue = propertyValue.Property.DefaultValue;
  655. propertyValue.IsDirty = true;
  656. }
  657. SetPropertyValues (context, values);
  658. }
  659. // FIXME: implement
  660. public void Upgrade (SettingsContext context, SettingsPropertyCollection properties)
  661. {
  662. }
  663. public static void setCreate ()
  664. {
  665. CreateUserConfigPath();
  666. }
  667. }
  668. }
  669. #endif