CustomizableFileSettingsProvider.cs 27 KB

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