CustomizableFileSettingsProvider.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. MethodInfo entryPoint = assembly.EntryPoint;
  214. Type entryType = entryPoint != null ? entryPoint.DeclaringType : null;
  215. if (entryType != null && entryType.Namespace.Length > 0) {
  216. int end = entryType.Namespace.IndexOf ('.');
  217. return end < 0 ? entryType.Namespace : entryType.Namespace.Substring (0, end);
  218. }
  219. return "Program";
  220. }
  221. private static string GetProductName ()
  222. {
  223. Assembly assembly = Assembly.GetEntryAssembly ();
  224. if (assembly == null)
  225. assembly = Assembly.GetCallingAssembly ();
  226. #if true
  227. byte [] pkt = assembly.GetName ().GetPublicKeyToken ();
  228. byte [] hash = SHA1.Create ().ComputeHash (pkt != null ? pkt : Encoding.UTF8.GetBytes (assembly.EscapedCodeBase));
  229. return String.Format ("{0}_{1}_{2}",
  230. AppDomain.CurrentDomain.FriendlyName,
  231. pkt != null ? "StrongName" : "Url",
  232. // FIXME: it seems that something else is used
  233. // here, to convert hash bytes to string.
  234. Convert.ToBase64String (hash));
  235. #else // AssemblyProductAttribute-based code
  236. AssemblyProductAttribute [] attrs = (AssemblyProductAttribute[]) assembly.GetCustomAttributes (typeof (AssemblyProductAttribute), true);
  237. if ((attrs != null) && attrs.Length > 0) {
  238. return attrs [0].Product;
  239. }
  240. return assembly.GetName ().Name;
  241. #endif
  242. }
  243. private static string GetProductVersion ()
  244. {
  245. Assembly assembly = Assembly.GetEntryAssembly ();
  246. if (assembly == null)
  247. assembly = Assembly.GetCallingAssembly ();
  248. if (assembly == null)
  249. return string.Empty;
  250. return assembly.GetName ().Version.ToString ();
  251. }
  252. private static void CreateUserConfigPath ()
  253. {
  254. if (userDefine)
  255. return;
  256. if (ProductName == "")
  257. ProductName = GetProductName ();
  258. if (CompanyName == "")
  259. CompanyName = GetCompanyName ();
  260. if (ForceVersion == "")
  261. ProductVersion = GetProductVersion ().Split('.');
  262. // C:\Documents and Settings\(user)\Application Data
  263. if (userRoamingBasePath == "")
  264. userRoamingPath = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
  265. else
  266. userRoamingPath = userRoamingBasePath;
  267. // C:\Documents and Settings\(user)\Local Settings\Application Data (on Windows)
  268. if (userLocalBasePath == "")
  269. userLocalPath = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData);
  270. else
  271. userLocalPath = userLocalBasePath;
  272. if (isCompany) {
  273. userRoamingPath = Path.Combine (userRoamingPath, CompanyName);
  274. userLocalPath = Path.Combine (userLocalPath, CompanyName);
  275. }
  276. if (isProduct) {
  277. userRoamingPath = Path.Combine (userRoamingPath, ProductName);
  278. userLocalPath = Path.Combine (userLocalPath, ProductName);
  279. }
  280. string versionName;
  281. if (ForceVersion == "") {
  282. if (isVersionRevision)
  283. versionName = String.Format ("{0}.{1}.{2}.{3}", ProductVersion [0], ProductVersion [1], ProductVersion [2], ProductVersion [3]);
  284. else if (isVersionBuild)
  285. versionName = String.Format ("{0}.{1}.{2}", ProductVersion [0], ProductVersion [1], ProductVersion [2]);
  286. else if (isVersionMinor)
  287. versionName = String.Format ("{0}.{1}", ProductVersion [0], ProductVersion [1]);
  288. else if (isVersionMajor)
  289. versionName = ProductVersion [0];
  290. else
  291. versionName = "";
  292. }
  293. else
  294. versionName = ForceVersion;
  295. string prevVersionRoaming = PrevVersionPath (userRoamingPath, versionName);
  296. string prevVersionLocal = PrevVersionPath (userLocalPath, versionName);
  297. userRoamingPath = Path.Combine (userRoamingPath, versionName);
  298. userLocalPath = Path.Combine (userLocalPath, versionName);
  299. if (prevVersionRoaming != "")
  300. userRoamingPathPrevVersion = Path.Combine(userRoamingPath, prevVersionRoaming);
  301. if (prevVersionLocal != "")
  302. userLocalPathPrevVersion = Path.Combine(userLocalPath, prevVersionLocal);
  303. }
  304. // string for the previous version. It ignores newer ones.
  305. private static string PrevVersionPath (string dirName, string currentVersion)
  306. {
  307. string prevVersionString = "";
  308. if (!Directory.Exists(dirName))
  309. return prevVersionString;
  310. DirectoryInfo currentDir = new DirectoryInfo (dirName);
  311. foreach (DirectoryInfo dirInfo in currentDir.GetDirectories ())
  312. if (String.Compare (currentVersion, dirInfo.Name, StringComparison.Ordinal) > 0)
  313. if (String.Compare (prevVersionString, dirInfo.Name, StringComparison.Ordinal) < 0)
  314. prevVersionString = dirInfo.Name;
  315. return prevVersionString;
  316. }
  317. // sets the explicit path to store roaming user.config or equivalent.
  318. // (returns the path validity.)
  319. public static bool SetUserRoamingPath (string configPath)
  320. {
  321. if (CheckPath (configPath))
  322. {
  323. userRoamingBasePath = configPath;
  324. return true;
  325. }
  326. else
  327. return false;
  328. }
  329. // sets the explicit path to store local user.config or equivalent.
  330. // (returns the path validity.)
  331. public static bool SetUserLocalPath (string configPath)
  332. {
  333. if (CheckPath (configPath))
  334. {
  335. userLocalBasePath = configPath;
  336. return true;
  337. }
  338. else
  339. return false;
  340. }
  341. private static bool CheckFileName (string configFile)
  342. {
  343. /*
  344. char[] invalidFileChars = Path.GetInvalidFileNameChars();
  345. foreach (char invalidChar in invalidFileChars)
  346. {
  347. if (configFile.Contains(invalidChar.ToString()))
  348. {
  349. return false;
  350. }
  351. }
  352. return true;
  353. */
  354. return configFile.IndexOfAny (Path.GetInvalidFileNameChars ()) < 0;
  355. }
  356. // sets the explicit roaming file name which is user.config equivalent.
  357. // (returns the file name validity.)
  358. public static bool SetUserRoamingFileName (string configFile)
  359. {
  360. if (CheckFileName (configFile))
  361. {
  362. userRoamingName = configFile;
  363. return true;
  364. }
  365. else
  366. return false;
  367. }
  368. // sets the explicit local file name which is user.config equivalent.
  369. // (returns the file name validity.)
  370. public static bool SetUserLocalFileName (string configFile)
  371. {
  372. if (CheckFileName (configFile))
  373. {
  374. userLocalName = configFile;
  375. return true;
  376. }
  377. else
  378. return false;
  379. }
  380. // sets the explicit company name for folder.
  381. // (returns the file name validity.)
  382. public static bool SetCompanyName (string companyName)
  383. {
  384. if (CheckFileName (companyName))
  385. {
  386. CompanyName = companyName;
  387. return true;
  388. }
  389. else
  390. return false;
  391. }
  392. // sets the explicit product name for folder.
  393. // (returns the file name validity.)
  394. public static bool SetProductName (string productName)
  395. {
  396. if (CheckFileName (productName))
  397. {
  398. ProductName = productName;
  399. return true;
  400. }
  401. else
  402. return false;
  403. }
  404. // sets the explicit major version for folder.
  405. public static bool SetVersion (int major)
  406. {
  407. ForceVersion = string.Format ("{0}", major);
  408. return true;
  409. }
  410. // sets the explicit major and minor versions for folder.
  411. public static bool SetVersion (int major, int minor)
  412. {
  413. ForceVersion = string.Format ("{0}.{1}", major, minor);
  414. return true;
  415. }
  416. // sets the explicit major/minor/build numbers for folder.
  417. public static bool SetVersion (int major, int minor, int build)
  418. {
  419. ForceVersion = string.Format ("{0}.{1}.{2}", major, minor, build);
  420. return true;
  421. }
  422. // sets the explicit major/minor/build/revision numbers for folder.
  423. public static bool SetVersion (int major, int minor, int build, int revision)
  424. {
  425. ForceVersion = string.Format ("{0}.{1}.{2}.{3}", major, minor, build, revision);
  426. return true;
  427. }
  428. // sets the explicit version number string for folder.
  429. public static bool SetVersion (string forceVersion)
  430. {
  431. if (CheckFileName (forceVersion))
  432. {
  433. ForceVersion = forceVersion;
  434. return true;
  435. }
  436. else
  437. return false;
  438. }
  439. private static bool CheckPath (string configPath)
  440. {
  441. char[] invalidPathChars = Path.GetInvalidPathChars ();
  442. /*
  443. foreach (char invalidChar in invalidPathChars)
  444. {
  445. if (configPath.Contains (invalidChar.ToString()))
  446. {
  447. return false;
  448. }
  449. }
  450. */
  451. if (configPath.IndexOfAny (invalidPathChars) >= 0)
  452. return false;
  453. string folder = configPath;
  454. string fileName;
  455. while ((fileName = Path.GetFileName (folder)) != "")
  456. {
  457. if (!CheckFileName (fileName))
  458. {
  459. return false;
  460. }
  461. folder = Path.GetDirectoryName (folder);
  462. }
  463. return true;
  464. }
  465. public override string Name {
  466. get { return base.Name; }
  467. }
  468. string app_name = String.Empty;//"OJK.CustomSetting.CustomizableLocalFileSettingsProvider";
  469. public override string ApplicationName {
  470. get { return app_name; }
  471. set { app_name = value; }
  472. }
  473. private ExeConfigurationFileMap exeMapCurrent = null;
  474. private ExeConfigurationFileMap exeMapPrev = null;
  475. private SettingsPropertyValueCollection values = null;
  476. private void SaveProperties (ExeConfigurationFileMap exeMap, SettingsPropertyValueCollection collection, ConfigurationUserLevel level, SettingsContext context, bool checkUserLevel)
  477. {
  478. Configuration config = ConfigurationManager.OpenMappedExeConfiguration (exeMap, level);
  479. UserSettingsGroup userGroup = config.GetSectionGroup ("userSettings") as UserSettingsGroup;
  480. bool isRoaming = (level == ConfigurationUserLevel.PerUserRoaming);
  481. #if true // my reimplementation
  482. if (userGroup == null) {
  483. userGroup = new UserSettingsGroup ();
  484. config.SectionGroups.Add ("userSettings", userGroup);
  485. ApplicationSettingsBase asb = context.CurrentSettings;
  486. ClientSettingsSection cs = new ClientSettingsSection ();
  487. userGroup.Sections.Add (asb.GetType ().FullName, cs);
  488. }
  489. bool hasChanges = false;
  490. foreach (ConfigurationSection section in userGroup.Sections) {
  491. ClientSettingsSection userSection = section as ClientSettingsSection;
  492. if (userSection == null)
  493. continue;
  494. XmlDocument doc = new XmlDocument ();
  495. foreach (SettingsPropertyValue value in collection) {
  496. if (checkUserLevel && value.Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
  497. continue;
  498. hasChanges = true;
  499. SettingElement element = userSection.Settings.Get (value.Name);
  500. if (element == null) {
  501. element = new SettingElement (value.Name, value.Property.SerializeAs);
  502. userSection.Settings.Add (element);
  503. }
  504. if (element.Value.ValueXml == null)
  505. element.Value.ValueXml = new XmlDocument ().CreateDocumentFragment ();
  506. doc = element.Value.ValueXml.OwnerDocument;
  507. switch (value.Property.SerializeAs) {
  508. case SettingsSerializeAs.Xml:
  509. element.Value.ValueXml.InnerXml = value.SerializedValue as string;
  510. break;
  511. case SettingsSerializeAs.String:
  512. element.Value.ValueXml.InnerText = value.SerializedValue as string;
  513. break;
  514. case SettingsSerializeAs.Binary:
  515. element.Value.ValueXml.InnerText = Convert.ToBase64String (value.SerializedValue as byte []);
  516. break;
  517. default:
  518. throw new NotImplementedException ();
  519. }
  520. }
  521. }
  522. if (hasChanges)
  523. config.Save (ConfigurationSaveMode.Minimal, true);
  524. #else // original impl. - likely buggy to miss some properties to save
  525. foreach (ConfigurationSection configSection in userGroup.Sections)
  526. {
  527. ClientSettingsSection userSection = configSection as ClientSettingsSection;
  528. if (userSection != null)
  529. {
  530. /*
  531. userSection.Settings.Clear();
  532. foreach (SettingsPropertyValue propertyValue in collection)
  533. {
  534. if (propertyValue.IsDirty)
  535. {
  536. SettingElement element = new SettingElement(propertyValue.Name, SettingsSerializeAs.String);
  537. element.Value.ValueXml = new XmlDocument();
  538. element.Value.ValueXml.InnerXml = (string)propertyValue.SerializedValue;
  539. userSection.Settings.Add(element);
  540. }
  541. }
  542. */
  543. foreach (SettingElement element in userSection.Settings)
  544. {
  545. if (collection [element.Name] != null) {
  546. if (collection [element.Name].Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
  547. continue;
  548. element.SerializeAs = SettingsSerializeAs.String;
  549. element.Value.ValueXml.InnerXml = (string) collection [element.Name].SerializedValue; ///Value = XmlElement
  550. }
  551. }
  552. }
  553. }
  554. config.Save (ConfigurationSaveMode.Minimal, true);
  555. #endif
  556. }
  557. private void LoadPropertyValue (SettingsPropertyCollection collection, SettingElement element, bool allowOverwrite)
  558. {
  559. SettingsProperty prop = collection [element.Name];
  560. SettingsPropertyValue value = new SettingsPropertyValue (prop);
  561. value.IsDirty = false;
  562. value.SerializedValue = element.Value.ValueXml != null ? element.Value.ValueXml.InnerText : prop.DefaultValue;
  563. try
  564. {
  565. if (allowOverwrite)
  566. values.Remove (element.Name);
  567. values.Add (value);
  568. } catch (ArgumentException) {
  569. throw new ConfigurationErrorsException ();
  570. }
  571. }
  572. private void LoadProperies (ExeConfigurationFileMap exeMap, SettingsPropertyCollection collection, ConfigurationUserLevel level, string sectionGroupName, bool allowOverwrite)
  573. {
  574. Configuration config = ConfigurationManager.OpenMappedExeConfiguration (exeMap,level);
  575. ConfigurationSectionGroup sectionGroup = config.GetSectionGroup (sectionGroupName);
  576. if (sectionGroup != null) {
  577. foreach (ConfigurationSection configSection in sectionGroup.Sections) {
  578. ClientSettingsSection clientSection = configSection as ClientSettingsSection;
  579. if (clientSection != null)
  580. foreach (SettingElement element in clientSection.Settings)
  581. LoadPropertyValue(collection, element, allowOverwrite);
  582. }
  583. }
  584. }
  585. public override void SetPropertyValues (SettingsContext context, SettingsPropertyValueCollection collection)
  586. {
  587. CreateExeMap ();
  588. if (UserLocalFullPath == UserRoamingFullPath)
  589. {
  590. SaveProperties (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoaming, context, false);
  591. } else {
  592. SaveProperties (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoaming, context, true);
  593. SaveProperties (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoamingAndLocal, context, true);
  594. }
  595. }
  596. public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection collection)
  597. {
  598. CreateExeMap ();
  599. if (values == null) {
  600. values = new SettingsPropertyValueCollection ();
  601. LoadProperies (exeMapCurrent, collection, ConfigurationUserLevel.None, "applicationSettings", false);
  602. LoadProperies (exeMapCurrent, collection, ConfigurationUserLevel.None, "userSettings", false);
  603. LoadProperies (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoaming, "userSettings", true);
  604. LoadProperies (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoamingAndLocal, "userSettings", true);
  605. // create default values if not exist
  606. foreach (SettingsProperty p in collection)
  607. if (values [p.Name] == null)
  608. values.Add (new SettingsPropertyValue (p));
  609. }
  610. return values;
  611. }
  612. /// creates an ExeConfigurationFileMap
  613. private void CreateExeMap ()
  614. {
  615. if (exeMapCurrent == null) {
  616. CreateUserConfigPath ();
  617. // current version
  618. exeMapCurrent = new ExeConfigurationFileMap ();
  619. // exeMapCurrent.ExeConfigFilename = System.Windows.Forms.Application.ExecutablePath + ".config";
  620. Assembly entry = Assembly.GetEntryAssembly () ?? Assembly.GetExecutingAssembly ();
  621. exeMapCurrent.ExeConfigFilename = entry.Location + ".config";
  622. exeMapCurrent.LocalUserConfigFilename = UserLocalFullPath;
  623. exeMapCurrent.RoamingUserConfigFilename = UserRoamingFullPath;
  624. // previous version
  625. if ((PrevUserLocalFullPath != "") && (PrevUserRoamingFullPath != ""))
  626. {
  627. exeMapPrev = new ExeConfigurationFileMap();
  628. // exeMapPrev.ExeConfigFilename = System.Windows.Forms.Application.ExecutablePath + ".config";
  629. exeMapPrev.ExeConfigFilename = entry.Location + ".config";
  630. exeMapPrev.LocalUserConfigFilename = PrevUserLocalFullPath;
  631. exeMapPrev.RoamingUserConfigFilename = PrevUserRoamingFullPath;
  632. }
  633. }
  634. }
  635. // FIXME: implement
  636. public SettingsPropertyValue GetPreviousVersion (SettingsContext context, SettingsProperty property)
  637. {
  638. return null;
  639. }
  640. public void Reset (SettingsContext context)
  641. {
  642. CreateExeMap ();
  643. foreach (SettingsPropertyValue propertyValue in values) {
  644. propertyValue.PropertyValue = propertyValue.Property.DefaultValue;
  645. propertyValue.IsDirty = true;
  646. }
  647. SetPropertyValues (context, values);
  648. }
  649. // FIXME: implement
  650. public void Upgrade (SettingsContext context, SettingsPropertyCollection properties)
  651. {
  652. }
  653. public static void setCreate ()
  654. {
  655. CreateUserConfigPath();
  656. }
  657. }
  658. }
  659. #endif