2
0

CustomizableFileSettingsProvider.cs 22 KB

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