CustomizableFileSettingsProvider.cs 23 KB

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