CustomizableFileSettingsProvider.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. //
  2. // CustomizableFileSettingsProvider.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 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.Globalization;
  38. using System.IO;
  39. using System.Reflection;
  40. using System.Security.Cryptography;
  41. using System.Text;
  42. using System.Xml;
  43. #if TARGET_JVM
  44. using NameValueCollection = System.Collections.Specialized.NameValueCollection;
  45. #else
  46. using NameValueCollection = PrebuiltSystem.System.Collections.Specialized.NameValueCollection;
  47. #endif
  48. namespace System.Configuration
  49. {
  50. // location to store user configuration settings.
  51. internal enum UserConfigLocationOption : uint
  52. {
  53. Product = 0x20,
  54. Product_VersionMajor = 0x21,
  55. Product_VersionMinor = 0x22,
  56. Product_VersionBuild = 0x24,
  57. Product_VersionRevision = 0x28,
  58. Company_Product = 0x30,
  59. Company_Product_VersionMajor = 0x31,
  60. Company_Product_VersionMinor = 0x32,
  61. Company_Product_VersionBuild = 0x34,
  62. Company_Product_VersionRevision = 0x38,
  63. Evidence = 0x40,
  64. Other = 0x8000
  65. }
  66. internal class CustomizableFileSettingsProvider : SettingsProvider, IApplicationSettingsProvider
  67. {
  68. // KLUDGE WARNING.
  69. //
  70. // This is used from within System.Web to allow mapping of the ExeConfigFilename to
  71. // the correct Web.config for the current request. Otherwise web applications will
  72. // not be able to access settings from Web.config. The type assigned to this
  73. // variable must descend from the ConfigurationFileMap class and its
  74. // MachineConfigFilename will be used to set the ExeConfigFilename.
  75. //
  76. // This is necessary to fix bug #491531
  77. #pragma warning disable 649
  78. private static Type webConfigurationFileMapType;
  79. #pragma warning restore 649
  80. private static string userRoamingPath = "";
  81. private static string userLocalPath = "";
  82. private static string userRoamingPathPrevVersion = "";
  83. private static string userLocalPathPrevVersion = "";
  84. private static string userRoamingName = "user.config";
  85. private static string userLocalName = "user.config";
  86. private static string userRoamingBasePath = "";
  87. private static string userLocalBasePath = "";
  88. private static string CompanyName = "";
  89. private static string ProductName = "";
  90. private static string ForceVersion = "";
  91. private static string[] ProductVersion;
  92. // whether to include parts in the folder name or not:
  93. private static bool isVersionMajor = false; // 0x0001 major version
  94. private static bool isVersionMinor = false; // 0x0002 minor version
  95. private static bool isVersionBuild = false; // 0x0004 build version
  96. private static bool isVersionRevision = false; // 0x0008 revision
  97. private static bool isCompany = true; // 0x0010 corporate name
  98. private static bool isProduct = true; // 0x0020 product name
  99. private static bool isEvidence = false; // 0x0040 evidence hash
  100. private static bool userDefine = false; // 0x8000 ignore all above and use user definition
  101. private static UserConfigLocationOption userConfig = UserConfigLocationOption.Company_Product;
  102. public override void Initialize (string name, NameValueCollection config)
  103. {
  104. base.Initialize (name, config);
  105. }
  106. // full path to roaming user.config
  107. internal static string UserRoamingFullPath {
  108. get { return Path.Combine (userRoamingPath, userRoamingName); }
  109. }
  110. // full path to local user.config
  111. internal static string UserLocalFullPath {
  112. get { return Path.Combine (userLocalPath, userLocalName); }
  113. }
  114. // previous full path to roaming user.config
  115. public static string PrevUserRoamingFullPath {
  116. get { return Path.Combine (userRoamingPathPrevVersion, userRoamingName); }
  117. }
  118. // previous full path to local user.config
  119. public static string PrevUserLocalFullPath {
  120. get { return Path.Combine (userLocalPathPrevVersion, userLocalName); }
  121. }
  122. // path to roaming user.config
  123. public static string UserRoamingPath {
  124. get { return userRoamingPath; }
  125. }
  126. // path to local user.config
  127. public static string UserLocalPath {
  128. get { return userLocalPath; }
  129. }
  130. // file name which is equivalent to user.config, for roaming user
  131. public static string UserRoamingName {
  132. get { return userRoamingName; }
  133. }
  134. // file name which is equivalent to user.config, for local user
  135. public static string UserLocalName {
  136. get { return userLocalName; }
  137. }
  138. public static UserConfigLocationOption UserConfigSelector
  139. {
  140. get { return userConfig; }
  141. set {
  142. userConfig = value;
  143. if (((uint) userConfig & 0x8000) != 0) {
  144. isVersionMajor = false;
  145. isVersionMinor = false;
  146. isVersionBuild = false;
  147. isVersionRevision = false;
  148. isCompany = false;
  149. return;
  150. }
  151. isVersionRevision = ((uint) userConfig & 0x0008) != 0;
  152. isVersionBuild = isVersionRevision | ((uint)userConfig & 0x0004) != 0;
  153. isVersionMinor = isVersionBuild | ((uint)userConfig & 0x0002) != 0;
  154. isVersionMajor = IsVersionMinor | ((uint)userConfig & 0x0001) != 0;
  155. isCompany = ((uint) userConfig & 0x0010) != 0;
  156. isProduct = ((uint) userConfig & 0x0020) != 0;
  157. }
  158. }
  159. // whether the path to include the major version.
  160. public static bool IsVersionMajor
  161. {
  162. get { return isVersionMajor; }
  163. set
  164. {
  165. isVersionMajor = value;
  166. isVersionMinor = false;
  167. isVersionBuild = false;
  168. isVersionRevision = false;
  169. }
  170. }
  171. // whether the path to include minor version.
  172. public static bool IsVersionMinor
  173. {
  174. get { return isVersionMinor; }
  175. set
  176. {
  177. isVersionMinor = value;
  178. if (isVersionMinor)
  179. isVersionMajor = true;
  180. isVersionBuild = false;
  181. isVersionRevision = false;
  182. }
  183. }
  184. // whether the path to include build version.
  185. public static bool IsVersionBuild
  186. {
  187. get { return isVersionBuild; }
  188. set
  189. {
  190. isVersionBuild = value;
  191. if (isVersionBuild) {
  192. isVersionMajor = true;
  193. isVersionMinor = true;
  194. }
  195. isVersionRevision = false;
  196. }
  197. }
  198. // whether the path to include revision.
  199. public static bool IsVersionRevision
  200. {
  201. get { return isVersionRevision; }
  202. set
  203. {
  204. isVersionRevision = value;
  205. if (isVersionRevision) {
  206. isVersionMajor = true;
  207. isVersionMinor = true;
  208. isVersionBuild = true;
  209. }
  210. }
  211. }
  212. // whether the path to include company name.
  213. public static bool IsCompany
  214. {
  215. get { return isCompany; }
  216. set { isCompany = value; }
  217. }
  218. // whether the path to include evidence hash.
  219. public static bool IsEvidence
  220. {
  221. get { return isEvidence; }
  222. set { isEvidence = value; }
  223. }
  224. // AssemblyCompanyAttribute->Namespace->"Program"
  225. private static string GetCompanyName ()
  226. {
  227. Assembly assembly = Assembly.GetEntryAssembly ();
  228. if (assembly == null)
  229. assembly = Assembly.GetCallingAssembly ();
  230. AssemblyCompanyAttribute [] attrs = (AssemblyCompanyAttribute []) assembly.GetCustomAttributes (typeof (AssemblyCompanyAttribute), true);
  231. if ((attrs != null) && attrs.Length > 0) {
  232. return attrs [0].Company;
  233. }
  234. #if !TARGET_JVM
  235. MethodInfo entryPoint = assembly.EntryPoint;
  236. Type entryType = entryPoint != null ? entryPoint.DeclaringType : null;
  237. if (entryType != null && !String.IsNullOrEmpty (entryType.Namespace)) {
  238. int end = entryType.Namespace.IndexOf ('.');
  239. return end < 0 ? entryType.Namespace : entryType.Namespace.Substring (0, end);
  240. }
  241. return "Program";
  242. #else
  243. return assembly.GetName ().Name;
  244. #endif
  245. }
  246. private static string GetProductName ()
  247. {
  248. Assembly assembly = Assembly.GetEntryAssembly ();
  249. if (assembly == null)
  250. assembly = Assembly.GetCallingAssembly ();
  251. #if !TARGET_JVM
  252. byte [] pkt = assembly.GetName ().GetPublicKeyToken ();
  253. return String.Format ("{0}_{1}_{2}",
  254. AppDomain.CurrentDomain.FriendlyName,
  255. pkt != null && pkt.Length > 0 ? "StrongName" : "Url",
  256. GetEvidenceHash());
  257. #else // AssemblyProductAttribute-based code
  258. AssemblyProductAttribute [] attrs = (AssemblyProductAttribute[]) assembly.GetCustomAttributes (typeof (AssemblyProductAttribute), true);
  259. if ((attrs != null) && attrs.Length > 0) {
  260. return attrs [0].Product;
  261. }
  262. return assembly.GetName ().Name;
  263. #endif
  264. }
  265. // Note: Changed from base64() to hex output to avoid unexpected chars like '\' or '/' with filesystem meaning.
  266. // Otherwise eventually filenames, which are invalid on linux or windows, might be created.
  267. // Signed-off-by: Carsten Schlote <[email protected]>
  268. // TODO: Compare with .NET. It might be also, that their way isn't suitable for Unix OS derivates (slahes in output)
  269. private static string GetEvidenceHash ()
  270. {
  271. Assembly assembly = Assembly.GetEntryAssembly ();
  272. if (assembly == null)
  273. assembly = Assembly.GetCallingAssembly ();
  274. byte [] pkt = assembly.GetName ().GetPublicKeyToken ();
  275. byte [] hash = SHA1.Create ().ComputeHash (pkt != null && pkt.Length >0 ? pkt : Encoding.UTF8.GetBytes (assembly.EscapedCodeBase));
  276. System.Text.StringBuilder evidence_string = new System.Text.StringBuilder();
  277. foreach (byte b in hash)
  278. evidence_string.AppendFormat("{0:x2}",b);
  279. return evidence_string.ToString ();
  280. }
  281. private static string GetProductVersion ()
  282. {
  283. Assembly assembly = Assembly.GetEntryAssembly ();
  284. if (assembly == null)
  285. assembly = Assembly.GetCallingAssembly ();
  286. if (assembly == null)
  287. return string.Empty;
  288. return assembly.GetName ().Version.ToString ();
  289. }
  290. private static void CreateUserConfigPath ()
  291. {
  292. if (userDefine)
  293. return;
  294. if (ProductName == "")
  295. ProductName = GetProductName ();
  296. if (CompanyName == "")
  297. CompanyName = GetCompanyName ();
  298. if (ForceVersion == "")
  299. ProductVersion = GetProductVersion ().Split('.');
  300. // C:\Documents and Settings\(user)\Application Data
  301. #if !TARGET_JVM
  302. if (userRoamingBasePath == "")
  303. userRoamingPath = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
  304. else
  305. #endif
  306. userRoamingPath = userRoamingBasePath;
  307. // C:\Documents and Settings\(user)\Local Settings\Application Data (on Windows)
  308. #if !TARGET_JVM
  309. if (userLocalBasePath == "")
  310. userLocalPath = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData);
  311. else
  312. #endif
  313. userLocalPath = userLocalBasePath;
  314. if (isCompany) {
  315. userRoamingPath = Path.Combine (userRoamingPath, CompanyName);
  316. userLocalPath = Path.Combine (userLocalPath, CompanyName);
  317. }
  318. if (isProduct) {
  319. if (isEvidence) {
  320. Assembly assembly = Assembly.GetEntryAssembly ();
  321. if (assembly == null)
  322. assembly = Assembly.GetCallingAssembly ();
  323. #if !TARGET_JVM
  324. byte [] pkt = assembly.GetName ().GetPublicKeyToken ();
  325. ProductName = String.Format ("{0}_{1}_{2}",
  326. ProductName,
  327. pkt != null ? "StrongName" : "Url",
  328. GetEvidenceHash());
  329. #endif
  330. }
  331. userRoamingPath = Path.Combine (userRoamingPath, ProductName);
  332. userLocalPath = Path.Combine (userLocalPath, ProductName);
  333. }
  334. string versionName;
  335. if (ForceVersion == "") {
  336. if (isVersionRevision)
  337. versionName = String.Format ("{0}.{1}.{2}.{3}", ProductVersion [0], ProductVersion [1], ProductVersion [2], ProductVersion [3]);
  338. else if (isVersionBuild)
  339. versionName = String.Format ("{0}.{1}.{2}", ProductVersion [0], ProductVersion [1], ProductVersion [2]);
  340. else if (isVersionMinor)
  341. versionName = String.Format ("{0}.{1}", ProductVersion [0], ProductVersion [1]);
  342. else if (isVersionMajor)
  343. versionName = ProductVersion [0];
  344. else
  345. versionName = "";
  346. }
  347. else
  348. versionName = ForceVersion;
  349. string prevVersionRoaming = PrevVersionPath (userRoamingPath, versionName);
  350. string prevVersionLocal = PrevVersionPath (userLocalPath, versionName);
  351. userRoamingPath = Path.Combine (userRoamingPath, versionName);
  352. userLocalPath = Path.Combine (userLocalPath, versionName);
  353. if (prevVersionRoaming != "")
  354. userRoamingPathPrevVersion = Path.Combine(userRoamingPath, prevVersionRoaming);
  355. if (prevVersionLocal != "")
  356. userLocalPathPrevVersion = Path.Combine(userLocalPath, prevVersionLocal);
  357. }
  358. // string for the previous version. It ignores newer ones.
  359. private static string PrevVersionPath (string dirName, string currentVersion)
  360. {
  361. string prevVersionString = "";
  362. if (!Directory.Exists(dirName))
  363. return prevVersionString;
  364. DirectoryInfo currentDir = new DirectoryInfo (dirName);
  365. foreach (DirectoryInfo dirInfo in currentDir.GetDirectories ())
  366. if (String.Compare (currentVersion, dirInfo.Name, StringComparison.Ordinal) > 0)
  367. if (String.Compare (prevVersionString, dirInfo.Name, StringComparison.Ordinal) < 0)
  368. prevVersionString = dirInfo.Name;
  369. return prevVersionString;
  370. }
  371. // sets the explicit path to store roaming user.config or equivalent.
  372. // (returns the path validity.)
  373. public static bool SetUserRoamingPath (string configPath)
  374. {
  375. if (CheckPath (configPath))
  376. {
  377. userRoamingBasePath = configPath;
  378. return true;
  379. }
  380. else
  381. return false;
  382. }
  383. // sets the explicit path to store local user.config or equivalent.
  384. // (returns the path validity.)
  385. public static bool SetUserLocalPath (string configPath)
  386. {
  387. if (CheckPath (configPath))
  388. {
  389. userLocalBasePath = configPath;
  390. return true;
  391. }
  392. else
  393. return false;
  394. }
  395. private static bool CheckFileName (string configFile)
  396. {
  397. /*
  398. char[] invalidFileChars = Path.GetInvalidFileNameChars();
  399. foreach (char invalidChar in invalidFileChars)
  400. {
  401. if (configFile.Contains(invalidChar.ToString()))
  402. {
  403. return false;
  404. }
  405. }
  406. return true;
  407. */
  408. return configFile.IndexOfAny (Path.GetInvalidFileNameChars ()) < 0;
  409. }
  410. // sets the explicit roaming file name which is user.config equivalent.
  411. // (returns the file name validity.)
  412. public static bool SetUserRoamingFileName (string configFile)
  413. {
  414. if (CheckFileName (configFile))
  415. {
  416. userRoamingName = configFile;
  417. return true;
  418. }
  419. else
  420. return false;
  421. }
  422. // sets the explicit local file name which is user.config equivalent.
  423. // (returns the file name validity.)
  424. public static bool SetUserLocalFileName (string configFile)
  425. {
  426. if (CheckFileName (configFile))
  427. {
  428. userLocalName = configFile;
  429. return true;
  430. }
  431. else
  432. return false;
  433. }
  434. // sets the explicit company name for folder.
  435. // (returns the file name validity.)
  436. public static bool SetCompanyName (string companyName)
  437. {
  438. if (CheckFileName (companyName))
  439. {
  440. CompanyName = companyName;
  441. return true;
  442. }
  443. else
  444. return false;
  445. }
  446. // sets the explicit product name for folder.
  447. // (returns the file name validity.)
  448. public static bool SetProductName (string productName)
  449. {
  450. if (CheckFileName (productName))
  451. {
  452. ProductName = productName;
  453. return true;
  454. }
  455. else
  456. return false;
  457. }
  458. // sets the explicit major version for folder.
  459. public static bool SetVersion (int major)
  460. {
  461. ForceVersion = string.Format ("{0}", major);
  462. return true;
  463. }
  464. // sets the explicit major and minor versions for folder.
  465. public static bool SetVersion (int major, int minor)
  466. {
  467. ForceVersion = string.Format ("{0}.{1}", major, minor);
  468. return true;
  469. }
  470. // sets the explicit major/minor/build numbers for folder.
  471. public static bool SetVersion (int major, int minor, int build)
  472. {
  473. ForceVersion = string.Format ("{0}.{1}.{2}", major, minor, build);
  474. return true;
  475. }
  476. // sets the explicit major/minor/build/revision numbers for folder.
  477. public static bool SetVersion (int major, int minor, int build, int revision)
  478. {
  479. ForceVersion = string.Format ("{0}.{1}.{2}.{3}", major, minor, build, revision);
  480. return true;
  481. }
  482. // sets the explicit version number string for folder.
  483. public static bool SetVersion (string forceVersion)
  484. {
  485. if (CheckFileName (forceVersion))
  486. {
  487. ForceVersion = forceVersion;
  488. return true;
  489. }
  490. else
  491. return false;
  492. }
  493. private static bool CheckPath (string configPath)
  494. {
  495. char[] invalidPathChars = Path.GetInvalidPathChars ();
  496. /*
  497. foreach (char invalidChar in invalidPathChars)
  498. {
  499. if (configPath.Contains (invalidChar.ToString()))
  500. {
  501. return false;
  502. }
  503. }
  504. */
  505. if (configPath.IndexOfAny (invalidPathChars) >= 0)
  506. return false;
  507. string folder = configPath;
  508. string fileName;
  509. while ((fileName = Path.GetFileName (folder)) != "")
  510. {
  511. if (!CheckFileName (fileName))
  512. {
  513. return false;
  514. }
  515. folder = Path.GetDirectoryName (folder);
  516. }
  517. return true;
  518. }
  519. public override string Name {
  520. get { return base.Name; }
  521. }
  522. string app_name = String.Empty;//"OJK.CustomSetting.CustomizableLocalFileSettingsProvider";
  523. public override string ApplicationName {
  524. get { return app_name; }
  525. set { app_name = value; }
  526. }
  527. private ExeConfigurationFileMap exeMapCurrent = null;
  528. private ExeConfigurationFileMap exeMapPrev = null;
  529. private SettingsPropertyValueCollection values = null;
  530. private void SaveProperties (ExeConfigurationFileMap exeMap, SettingsPropertyValueCollection collection, ConfigurationUserLevel level, SettingsContext context, bool checkUserLevel)
  531. {
  532. Configuration config = ConfigurationManager.OpenMappedExeConfiguration (exeMap, level);
  533. UserSettingsGroup userGroup = config.GetSectionGroup ("userSettings") as UserSettingsGroup;
  534. bool isRoaming = (level == ConfigurationUserLevel.PerUserRoaming);
  535. #if true // my reimplementation
  536. if (userGroup == null) {
  537. userGroup = new UserSettingsGroup ();
  538. config.SectionGroups.Add ("userSettings", userGroup);
  539. }
  540. ApplicationSettingsBase asb = context.CurrentSettings;
  541. string class_name = NormalizeInvalidXmlChars ((asb != null ? asb.GetType () : typeof (ApplicationSettingsBase)).FullName);
  542. ClientSettingsSection userSection = null;
  543. ConfigurationSection cnf = userGroup.Sections.Get (class_name);
  544. userSection = cnf as ClientSettingsSection;
  545. if (userSection == null) {
  546. userSection = new ClientSettingsSection ();
  547. userGroup.Sections.Add (class_name, userSection);
  548. }
  549. bool hasChanges = false;
  550. if (userSection == null)
  551. return;
  552. foreach (SettingsPropertyValue value in collection) {
  553. if (checkUserLevel && value.Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
  554. continue;
  555. // The default impl does not save the ApplicationScopedSetting properties
  556. if (value.Property.Attributes.Contains (typeof (ApplicationScopedSettingAttribute)))
  557. continue;
  558. hasChanges = true;
  559. SettingElement element = userSection.Settings.Get (value.Name);
  560. if (element == null) {
  561. element = new SettingElement (value.Name, value.Property.SerializeAs);
  562. userSection.Settings.Add (element);
  563. }
  564. if (element.Value.ValueXml == null)
  565. element.Value.ValueXml = new XmlDocument ().CreateElement ("value");
  566. switch (value.Property.SerializeAs) {
  567. case SettingsSerializeAs.Xml:
  568. element.Value.ValueXml.InnerXml = (value.SerializedValue as string) ?? string.Empty;
  569. break;
  570. case SettingsSerializeAs.String:
  571. element.Value.ValueXml.InnerText = value.SerializedValue as string;
  572. break;
  573. case SettingsSerializeAs.Binary:
  574. element.Value.ValueXml.InnerText = value.SerializedValue != null ? Convert.ToBase64String (value.SerializedValue as byte []) : string.Empty;
  575. break;
  576. default:
  577. throw new NotImplementedException ();
  578. }
  579. }
  580. if (hasChanges)
  581. config.Save (ConfigurationSaveMode.Minimal, true);
  582. #else // original impl. - likely buggy to miss some properties to save
  583. foreach (ConfigurationSection configSection in userGroup.Sections)
  584. {
  585. ClientSettingsSection userSection = configSection as ClientSettingsSection;
  586. if (userSection != null)
  587. {
  588. /*
  589. userSection.Settings.Clear();
  590. foreach (SettingsPropertyValue propertyValue in collection)
  591. {
  592. if (propertyValue.IsDirty)
  593. {
  594. SettingElement element = new SettingElement(propertyValue.Name, SettingsSerializeAs.String);
  595. element.Value.ValueXml = new XmlDocument();
  596. element.Value.ValueXml.InnerXml = (string)propertyValue.SerializedValue;
  597. userSection.Settings.Add(element);
  598. }
  599. }
  600. */
  601. foreach (SettingElement element in userSection.Settings)
  602. {
  603. if (collection [element.Name] != null) {
  604. if (collection [element.Name].Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
  605. continue;
  606. element.SerializeAs = SettingsSerializeAs.String;
  607. element.Value.ValueXml.InnerXml = (string) collection [element.Name].SerializedValue; ///Value = XmlElement
  608. }
  609. }
  610. }
  611. }
  612. config.Save (ConfigurationSaveMode.Minimal, true);
  613. #endif
  614. }
  615. // NOTE: We should add here all the chars that are valid in a name of a class (Ecma-wise),
  616. // but invalid in an xml element name, and provide a better impl if we get too many of them.
  617. string NormalizeInvalidXmlChars (string str)
  618. {
  619. char [] invalid_chars = new char [] { '+' };
  620. if (str == null || str.IndexOfAny (invalid_chars) == -1)
  621. return str;
  622. // Replace with its hexadecimal values.
  623. str = str.Replace ("+", "_x002B_");
  624. return str;
  625. }
  626. private void LoadPropertyValue (SettingsPropertyCollection collection, SettingElement element, bool allowOverwrite)
  627. {
  628. SettingsProperty prop = collection [element.Name];
  629. if (prop == null) { // see bug #343459
  630. prop = new SettingsProperty (element.Name);
  631. collection.Add (prop);
  632. }
  633. SettingsPropertyValue value = new SettingsPropertyValue (prop);
  634. value.IsDirty = false;
  635. if (element.Value.ValueXml != null) {
  636. switch (value.Property.SerializeAs) {
  637. case SettingsSerializeAs.Xml:
  638. value.SerializedValue = element.Value.ValueXml.InnerXml;
  639. break;
  640. case SettingsSerializeAs.String:
  641. value.SerializedValue = element.Value.ValueXml.InnerText;
  642. break;
  643. case SettingsSerializeAs.Binary:
  644. value.SerializedValue = Convert.FromBase64String (element.Value.ValueXml.InnerText);
  645. break;
  646. }
  647. }
  648. else
  649. value.SerializedValue = prop.DefaultValue;
  650. try
  651. {
  652. if (allowOverwrite)
  653. values.Remove (element.Name);
  654. values.Add (value);
  655. } catch (ArgumentException ex) {
  656. throw new ConfigurationErrorsException (string.Format (
  657. CultureInfo.InvariantCulture,
  658. "Failed to load value for '{0}'.",
  659. element.Name), ex);
  660. }
  661. }
  662. private void LoadProperties (ExeConfigurationFileMap exeMap, SettingsPropertyCollection collection, ConfigurationUserLevel level, string sectionGroupName, bool allowOverwrite, string groupName)
  663. {
  664. Configuration config = ConfigurationManager.OpenMappedExeConfiguration (exeMap,level);
  665. ConfigurationSectionGroup sectionGroup = config.GetSectionGroup (sectionGroupName);
  666. if (sectionGroup != null) {
  667. foreach (ConfigurationSection configSection in sectionGroup.Sections) {
  668. if (configSection.SectionInformation.Name != groupName)
  669. continue;
  670. ClientSettingsSection clientSection = configSection as ClientSettingsSection;
  671. if (clientSection == null)
  672. continue;
  673. foreach (SettingElement element in clientSection.Settings) {
  674. LoadPropertyValue(collection, element, allowOverwrite);
  675. }
  676. // Only the first one seems to be processed by MS
  677. break;
  678. }
  679. }
  680. }
  681. public override void SetPropertyValues (SettingsContext context, SettingsPropertyValueCollection collection)
  682. {
  683. CreateExeMap ();
  684. if (UserLocalFullPath == UserRoamingFullPath)
  685. {
  686. SaveProperties (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoaming, context, false);
  687. } else {
  688. SaveProperties (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoaming, context, true);
  689. SaveProperties (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoamingAndLocal, context, true);
  690. }
  691. }
  692. public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection collection)
  693. {
  694. CreateExeMap ();
  695. values = new SettingsPropertyValueCollection ();
  696. string groupName = context ["GroupName"] as string;
  697. groupName = NormalizeInvalidXmlChars (groupName); // we likely saved the element removing the non valid xml chars.
  698. LoadProperties (exeMapCurrent, collection, ConfigurationUserLevel.None, "applicationSettings", false, groupName);
  699. LoadProperties (exeMapCurrent, collection, ConfigurationUserLevel.None, "userSettings", false, groupName);
  700. LoadProperties (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoaming, "userSettings", true, groupName);
  701. LoadProperties (exeMapCurrent, collection, ConfigurationUserLevel.PerUserRoamingAndLocal, "userSettings", true, groupName);
  702. // create default values if not exist
  703. foreach (SettingsProperty p in collection)
  704. if (values [p.Name] == null)
  705. values.Add (new SettingsPropertyValue (p));
  706. return values;
  707. }
  708. /// creates an ExeConfigurationFileMap
  709. private void CreateExeMap ()
  710. {
  711. if (exeMapCurrent == null) {
  712. CreateUserConfigPath ();
  713. // current version
  714. exeMapCurrent = new ExeConfigurationFileMap ();
  715. // exeMapCurrent.ExeConfigFilename = System.Windows.Forms.Application.ExecutablePath + ".config";
  716. Assembly entry = Assembly.GetEntryAssembly () ?? Assembly.GetExecutingAssembly ();
  717. exeMapCurrent.ExeConfigFilename = entry.Location + ".config";
  718. exeMapCurrent.LocalUserConfigFilename = UserLocalFullPath;
  719. exeMapCurrent.RoamingUserConfigFilename = UserRoamingFullPath;
  720. if (webConfigurationFileMapType != null && typeof (ConfigurationFileMap).IsAssignableFrom (webConfigurationFileMapType)) {
  721. try {
  722. ConfigurationFileMap cfgFileMap = Activator.CreateInstance (webConfigurationFileMapType) as ConfigurationFileMap;
  723. if (cfgFileMap != null) {
  724. string fpath = cfgFileMap.MachineConfigFilename;
  725. if (!String.IsNullOrEmpty (fpath))
  726. exeMapCurrent.ExeConfigFilename = fpath;
  727. }
  728. } catch {
  729. // ignore
  730. }
  731. }
  732. // previous version
  733. if ((PrevUserLocalFullPath != "") && (PrevUserRoamingFullPath != ""))
  734. {
  735. exeMapPrev = new ExeConfigurationFileMap();
  736. // exeMapPrev.ExeConfigFilename = System.Windows.Forms.Application.ExecutablePath + ".config";
  737. exeMapPrev.ExeConfigFilename = entry.Location + ".config";
  738. exeMapPrev.LocalUserConfigFilename = PrevUserLocalFullPath;
  739. exeMapPrev.RoamingUserConfigFilename = PrevUserRoamingFullPath;
  740. }
  741. }
  742. }
  743. // FIXME: implement
  744. public SettingsPropertyValue GetPreviousVersion (SettingsContext context, SettingsProperty property)
  745. {
  746. return null;
  747. }
  748. public void Reset (SettingsContext context)
  749. {
  750. if (values != null) {
  751. foreach (SettingsPropertyValue propertyValue in values) {
  752. // Can't use propertyValue.Property.DefaultValue
  753. // as it may cause InvalidCastException (see bug# 532180)
  754. values[propertyValue.Name].PropertyValue = propertyValue.Reset ();
  755. }
  756. }
  757. }
  758. // FIXME: implement
  759. public void Upgrade (SettingsContext context, SettingsPropertyCollection properties)
  760. {
  761. }
  762. public static void setCreate ()
  763. {
  764. CreateUserConfigPath();
  765. }
  766. }
  767. }
  768. #endif