IsolatedStorageFile.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. //
  2. // System.IO.IsolatedStorage.IsolatedStorageFile
  3. //
  4. // Authors
  5. // Jonathan Pryor ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2003 Jonathan Pryor
  9. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Collections;
  31. using System.Reflection;
  32. using System.Runtime.InteropServices;
  33. using System.Runtime.Serialization.Formatters.Binary;
  34. using System.Security;
  35. using System.Security.Cryptography;
  36. using System.Security.Permissions;
  37. using System.Security.Policy;
  38. using System.Text;
  39. using Mono.Security.Cryptography;
  40. namespace System.IO.IsolatedStorage {
  41. // This is a terribly named class. It doesn't actually represent a file as
  42. // much as a directory
  43. #if NET_2_0
  44. [ComVisible (true)]
  45. #endif
  46. // FIXME: Further limit the assertion when imperative Assert is implemented
  47. [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
  48. public sealed class IsolatedStorageFile : IsolatedStorage, IDisposable {
  49. private bool _resolved;
  50. private ulong _maxSize;
  51. private Evidence _fullEvidences;
  52. public static IEnumerator GetEnumerator (IsolatedStorageScope scope)
  53. {
  54. Demand (scope);
  55. switch (scope) {
  56. case IsolatedStorageScope.User:
  57. case IsolatedStorageScope.User | IsolatedStorageScope.Roaming:
  58. #if NET_2_0
  59. case IsolatedStorageScope.Machine:
  60. #endif
  61. break;
  62. default:
  63. string msg = Locale.GetText ("Invalid scope, only User, User|Roaming and Machine are valid");
  64. throw new ArgumentException (msg);
  65. }
  66. return new IsolatedStorageFileEnumerator (scope, GetIsolatedStorageRoot (scope));
  67. }
  68. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope,
  69. Evidence domainEvidence, Type domainEvidenceType,
  70. Evidence assemblyEvidence, Type assemblyEvidenceType)
  71. {
  72. Demand (scope);
  73. bool domain = ((scope & IsolatedStorageScope.Domain) != 0);
  74. if (domain && (domainEvidence == null))
  75. throw new ArgumentNullException ("domainEvidence");
  76. bool assembly = ((scope & IsolatedStorageScope.Assembly) != 0);
  77. if (assembly && (assemblyEvidence == null))
  78. throw new ArgumentNullException ("assemblyEvidence");
  79. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  80. if (domain) {
  81. if (domainEvidenceType == null) {
  82. storageFile._domainIdentity = GetDomainIdentityFromEvidence (domainEvidence);
  83. } else {
  84. storageFile._domainIdentity = GetTypeFromEvidence (domainEvidence, domainEvidenceType);
  85. }
  86. if (storageFile._domainIdentity == null)
  87. throw new IsolatedStorageException (Locale.GetText ("Couldn't find domain identity."));
  88. }
  89. if (assembly) {
  90. if (assemblyEvidenceType == null) {
  91. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (assemblyEvidence);
  92. } else {
  93. storageFile._assemblyIdentity = GetTypeFromEvidence (assemblyEvidence, assemblyEvidenceType);
  94. }
  95. if (storageFile._assemblyIdentity == null)
  96. throw new IsolatedStorageException (Locale.GetText ("Couldn't find assembly identity."));
  97. }
  98. storageFile.PostInit ();
  99. return storageFile;
  100. }
  101. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope, object domainIdentity, object assemblyIdentity)
  102. {
  103. Demand (scope);
  104. if (((scope & IsolatedStorageScope.Domain) != 0) && (domainIdentity == null))
  105. throw new ArgumentNullException ("domainIdentity");
  106. bool assembly = ((scope & IsolatedStorageScope.Assembly) != 0);
  107. if (assembly && (assemblyIdentity == null))
  108. throw new ArgumentNullException ("assemblyIdentity");
  109. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  110. if (assembly)
  111. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  112. storageFile._domainIdentity = domainIdentity;
  113. storageFile._assemblyIdentity = assemblyIdentity;
  114. storageFile.PostInit ();
  115. return storageFile;
  116. }
  117. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType)
  118. {
  119. Demand (scope);
  120. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  121. if ((scope & IsolatedStorageScope.Domain) != 0) {
  122. storageFile._domainIdentity = GetTypeFromEvidence (AppDomain.CurrentDomain.Evidence, domainEvidenceType);
  123. }
  124. if ((scope & IsolatedStorageScope.Assembly) != 0) {
  125. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  126. storageFile._fullEvidences = e;
  127. storageFile._assemblyIdentity = GetTypeFromEvidence (e, assemblyEvidenceType);
  128. }
  129. storageFile.PostInit ();
  130. return storageFile;
  131. }
  132. #if NET_2_0
  133. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope, object applicationIdentity)
  134. {
  135. Demand (scope);
  136. if (applicationIdentity == null)
  137. throw new ArgumentNullException ("applicationIdentity");
  138. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  139. storageFile._applicationIdentity = applicationIdentity;
  140. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  141. storageFile.PostInit ();
  142. return storageFile;
  143. }
  144. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope, Type applicationEvidenceType)
  145. {
  146. Demand (scope);
  147. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  148. storageFile.InitStore (scope, applicationEvidenceType);
  149. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  150. storageFile.PostInit ();
  151. return storageFile;
  152. }
  153. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByMachine)]
  154. public static IsolatedStorageFile GetMachineStoreForApplication ()
  155. {
  156. IsolatedStorageScope scope = IsolatedStorageScope.Machine | IsolatedStorageScope.Application;
  157. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  158. storageFile.InitStore (scope, null);
  159. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  160. storageFile.PostInit ();
  161. return storageFile;
  162. }
  163. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByMachine)]
  164. public static IsolatedStorageFile GetMachineStoreForAssembly ()
  165. {
  166. IsolatedStorageScope scope = IsolatedStorageScope.Machine | IsolatedStorageScope.Assembly;
  167. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  168. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  169. storageFile._fullEvidences = e;
  170. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  171. storageFile.PostInit ();
  172. return storageFile;
  173. }
  174. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.DomainIsolationByMachine)]
  175. public static IsolatedStorageFile GetMachineStoreForDomain ()
  176. {
  177. IsolatedStorageScope scope = IsolatedStorageScope.Machine | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
  178. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  179. storageFile._domainIdentity = GetDomainIdentityFromEvidence (AppDomain.CurrentDomain.Evidence);
  180. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  181. storageFile._fullEvidences = e;
  182. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  183. storageFile.PostInit ();
  184. return storageFile;
  185. }
  186. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByUser)]
  187. public static IsolatedStorageFile GetUserStoreForApplication ()
  188. {
  189. IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
  190. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  191. storageFile.InitStore (scope, null);
  192. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  193. storageFile.PostInit ();
  194. return storageFile;
  195. }
  196. #endif
  197. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser)]
  198. public static IsolatedStorageFile GetUserStoreForAssembly ()
  199. {
  200. IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
  201. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  202. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  203. storageFile._fullEvidences = e;
  204. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  205. storageFile.PostInit ();
  206. return storageFile;
  207. }
  208. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.DomainIsolationByUser)]
  209. public static IsolatedStorageFile GetUserStoreForDomain ()
  210. {
  211. IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
  212. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  213. storageFile._domainIdentity = GetDomainIdentityFromEvidence (AppDomain.CurrentDomain.Evidence);
  214. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  215. storageFile._fullEvidences = e;
  216. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  217. storageFile.PostInit ();
  218. return storageFile;
  219. }
  220. public static void Remove (IsolatedStorageScope scope)
  221. {
  222. string dir = GetIsolatedStorageRoot (scope);
  223. Directory.Delete (dir, true);
  224. }
  225. // internal static stuff
  226. // Security Note: We're using InternalGetFolderPath because
  227. // IsolatedStorage must be able to work even if we do not have
  228. // FileIOPermission's PathDiscovery permissions
  229. internal static string GetIsolatedStorageRoot (IsolatedStorageScope scope)
  230. {
  231. // IsolatedStorageScope mixes several flags into one.
  232. // This first level deals with the root directory - it
  233. // is decided based on User, User+Roaming or Machine
  234. string root = null;
  235. if ((scope & IsolatedStorageScope.User) != 0) {
  236. if ((scope & IsolatedStorageScope.Roaming) != 0) {
  237. root = Environment.InternalGetFolderPath (Environment.SpecialFolder.LocalApplicationData);
  238. } else {
  239. root = Environment.InternalGetFolderPath (Environment.SpecialFolder.ApplicationData);
  240. }
  241. #if NET_2_0
  242. } else if ((scope & IsolatedStorageScope.Machine) != 0) {
  243. root = Environment.InternalGetFolderPath (Environment.SpecialFolder.CommonApplicationData);
  244. #endif
  245. }
  246. if (root == null) {
  247. string msg = Locale.GetText ("Couldn't access storage location for '{0}'.");
  248. throw new IsolatedStorageException (String.Format (msg, scope));
  249. }
  250. return Path.Combine (root, ".isolated-storage");
  251. }
  252. private static void Demand (IsolatedStorageScope scope)
  253. {
  254. if (SecurityManager.SecurityEnabled) {
  255. IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission (PermissionState.None);
  256. isfp.UsageAllowed = ScopeToContainment (scope);
  257. isfp.Demand ();
  258. }
  259. }
  260. private static IsolatedStorageContainment ScopeToContainment (IsolatedStorageScope scope)
  261. {
  262. switch (scope) {
  263. case IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly | IsolatedStorageScope.User:
  264. return IsolatedStorageContainment.DomainIsolationByUser;
  265. case IsolatedStorageScope.Assembly | IsolatedStorageScope.User:
  266. return IsolatedStorageContainment.AssemblyIsolationByUser;
  267. case IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly | IsolatedStorageScope.User | IsolatedStorageScope.Roaming:
  268. return IsolatedStorageContainment.DomainIsolationByRoamingUser;
  269. case IsolatedStorageScope.Assembly | IsolatedStorageScope.User | IsolatedStorageScope.Roaming:
  270. return IsolatedStorageContainment.AssemblyIsolationByRoamingUser;
  271. #if NET_2_0
  272. case IsolatedStorageScope.Application | IsolatedStorageScope.User:
  273. return IsolatedStorageContainment.ApplicationIsolationByUser;
  274. case IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly | IsolatedStorageScope.Machine:
  275. return IsolatedStorageContainment.DomainIsolationByMachine;
  276. case IsolatedStorageScope.Assembly | IsolatedStorageScope.Machine:
  277. return IsolatedStorageContainment.AssemblyIsolationByMachine;
  278. case IsolatedStorageScope.Application | IsolatedStorageScope.Machine:
  279. return IsolatedStorageContainment.ApplicationIsolationByMachine;
  280. case IsolatedStorageScope.Application | IsolatedStorageScope.User | IsolatedStorageScope.Roaming:
  281. return IsolatedStorageContainment.ApplicationIsolationByRoamingUser;
  282. #endif
  283. default:
  284. // unknown ?!?! then ask for maximum (unrestricted)
  285. return IsolatedStorageContainment.UnrestrictedIsolatedStorage;
  286. }
  287. }
  288. internal static ulong GetDirectorySize (DirectoryInfo di)
  289. {
  290. ulong size = 0;
  291. foreach (FileInfo fi in di.GetFiles ())
  292. size += (ulong) fi.Length;
  293. foreach (DirectoryInfo d in di.GetDirectories ())
  294. size += GetDirectorySize (d);
  295. return size;
  296. }
  297. // non-static stuff
  298. private DirectoryInfo directory;
  299. private IsolatedStorageFile (IsolatedStorageScope scope)
  300. {
  301. storage_scope = scope;
  302. }
  303. internal IsolatedStorageFile (IsolatedStorageScope scope, string location)
  304. {
  305. storage_scope = scope;
  306. directory = new DirectoryInfo (location);
  307. if (!directory.Exists) {
  308. string msg = Locale.GetText ("Invalid storage.");
  309. throw new IsolatedStorageException (msg);
  310. }
  311. // load the identities
  312. }
  313. ~IsolatedStorageFile ()
  314. {
  315. }
  316. private void PostInit ()
  317. {
  318. string root = GetIsolatedStorageRoot (Scope);
  319. string dir = null;
  320. #if NET_2_0
  321. if (_applicationIdentity != null) {
  322. dir = String.Format ("a{0}{1}", SeparatorInternal, GetNameFromIdentity (_applicationIdentity));
  323. } else
  324. #endif
  325. if (_domainIdentity != null) {
  326. dir = String.Format ("d{0}{1}{0}{2}", SeparatorInternal,
  327. GetNameFromIdentity (_domainIdentity), GetNameFromIdentity (_assemblyIdentity));
  328. } else if (_assemblyIdentity != null) {
  329. dir = String.Format ("d{0}none{0}{1}", SeparatorInternal, GetNameFromIdentity (_assemblyIdentity));
  330. } else {
  331. throw new IsolatedStorageException (Locale.GetText ("No code identity available."));
  332. }
  333. root = Path.Combine (root, dir);
  334. // identities have been selected
  335. directory = new DirectoryInfo (root);
  336. if (!directory.Exists) {
  337. directory.Create ();
  338. SaveIdentities (root);
  339. }
  340. }
  341. [CLSCompliant(false)]
  342. public override ulong CurrentSize {
  343. get { return GetDirectorySize (directory); }
  344. }
  345. [CLSCompliant(false)]
  346. public override ulong MaximumSize {
  347. // return an ulong but default is signed long
  348. get {
  349. if (!SecurityManager.SecurityEnabled)
  350. return Int64.MaxValue;
  351. if (_resolved)
  352. return _maxSize;
  353. Evidence e = null;
  354. if (_fullEvidences != null) {
  355. // if possible use the complete evidences we had
  356. // for computing the X identity
  357. e = _fullEvidences;
  358. } else {
  359. e = new Evidence ();
  360. // otherwise use what was provided
  361. if (_assemblyIdentity != null)
  362. e.AddHost (_assemblyIdentity);
  363. }
  364. if (e.Count < 1) {
  365. throw new InvalidOperationException (
  366. Locale.GetText ("Couldn't get the quota from the available evidences."));
  367. }
  368. PermissionSet denied = null;
  369. PermissionSet ps = SecurityManager.ResolvePolicy (e, null, null, null, out denied);
  370. IsolatedStoragePermission isp = GetPermission (ps);
  371. if (isp == null) {
  372. if (ps.IsUnrestricted ()) {
  373. _maxSize = Int64.MaxValue; /* default value */
  374. } else {
  375. throw new InvalidOperationException (
  376. Locale.GetText ("No quota from the available evidences."));
  377. }
  378. } else {
  379. _maxSize = (ulong) isp.UserQuota;
  380. }
  381. _resolved = true;
  382. return _maxSize;
  383. }
  384. }
  385. internal string Root {
  386. get { return directory.FullName; }
  387. }
  388. // methods
  389. public void Close ()
  390. {
  391. }
  392. public void CreateDirectory (string dir)
  393. {
  394. directory.CreateSubdirectory (dir);
  395. }
  396. public void DeleteDirectory (string dir)
  397. {
  398. DirectoryInfo subdir = directory.CreateSubdirectory (dir);
  399. subdir.Delete ();
  400. }
  401. public void DeleteFile (string file)
  402. {
  403. File.Delete (Path.Combine (directory.FullName, file));
  404. }
  405. public void Dispose ()
  406. {
  407. // nothing to dispose, anyway we want to please the tools
  408. GC.SuppressFinalize (this);
  409. }
  410. public string[] GetDirectoryNames (string searchPattern)
  411. {
  412. DirectoryInfo[] adi = directory.GetDirectories (searchPattern);
  413. return GetNames (adi);
  414. }
  415. private string[] GetNames (FileSystemInfo[] afsi)
  416. {
  417. string[] r = new string[afsi.Length];
  418. for (int i = 0; i != afsi.Length; ++i)
  419. r[i] = afsi[i].Name;
  420. return r;
  421. }
  422. public string[] GetFileNames (string searchPattern)
  423. {
  424. FileInfo[] afi = directory.GetFiles (searchPattern);
  425. return GetNames (afi);
  426. }
  427. public override void Remove ()
  428. {
  429. directory.Delete (true);
  430. }
  431. protected override IsolatedStoragePermission GetPermission (PermissionSet ps)
  432. {
  433. if (ps == null)
  434. return null;
  435. return (IsolatedStoragePermission) ps.GetPermission (typeof (IsolatedStorageFilePermission));
  436. }
  437. // internal stuff
  438. private string GetNameFromIdentity (object identity)
  439. {
  440. // Note: Default evidences return an XML string with ToString
  441. byte[] id = Encoding.UTF8.GetBytes (identity.ToString ());
  442. SHA1 hash = SHA1.Create ();
  443. // this create an unique name for an identity - bad identities like Url
  444. // results in bad (i.e. changing) names.
  445. byte[] full = hash.ComputeHash (id, 0, id.Length);
  446. byte[] half = new byte [10];
  447. Buffer.BlockCopy (full, 0, half, 0, half.Length);
  448. return CryptoConvert.ToHex (half);
  449. }
  450. private static object GetTypeFromEvidence (Evidence e, Type t)
  451. {
  452. foreach (object o in e) {
  453. if (o.GetType () == t)
  454. return o;
  455. }
  456. return null;
  457. }
  458. internal static object GetAssemblyIdentityFromEvidence (Evidence e)
  459. {
  460. // we prefer...
  461. // a. a Publisher evidence
  462. object identity = GetTypeFromEvidence (e, typeof (Publisher));
  463. if (identity != null)
  464. return identity;
  465. // b. a StrongName evidence
  466. identity = GetTypeFromEvidence (e, typeof (StrongName));
  467. if (identity != null)
  468. return identity;
  469. // c. a Url evidence
  470. return GetTypeFromEvidence (e, typeof (Url));
  471. }
  472. internal static object GetDomainIdentityFromEvidence (Evidence e)
  473. {
  474. // we prefer...
  475. // a. a ApplicationDirectory evidence
  476. object identity = GetTypeFromEvidence (e, typeof (ApplicationDirectory));
  477. if (identity != null)
  478. return identity;
  479. // b. a Url evidence
  480. return GetTypeFromEvidence (e, typeof (Url));
  481. }
  482. [Serializable]
  483. private struct Identities {
  484. public object Application;
  485. public object Assembly;
  486. public object Domain;
  487. public Identities (object application, object assembly, object domain)
  488. {
  489. Application = application;
  490. Assembly = assembly;
  491. Domain = domain;
  492. }
  493. }
  494. private void LoadIdentities (string root)
  495. {
  496. if (!File.Exists (root + ".storage"))
  497. throw new IsolatedStorageException (Locale.GetText ("Missing identities."));
  498. using (FileStream fs = File.OpenRead (root + ".storage")) {
  499. BinaryFormatter deformatter = new BinaryFormatter ();
  500. Identities identities = (Identities) deformatter.Deserialize (fs);
  501. fs.Close ();
  502. _applicationIdentity = identities.Application;
  503. _assemblyIdentity = identities.Assembly;
  504. _domainIdentity = identities.Domain;
  505. }
  506. }
  507. private void SaveIdentities (string root)
  508. {
  509. Identities identities = new Identities (_applicationIdentity, _assemblyIdentity, _domainIdentity);
  510. using (FileStream fs = File.OpenWrite (root + ".storage")) {
  511. BinaryFormatter formatter = new BinaryFormatter ();
  512. formatter.Serialize (fs, identities);
  513. fs.Close ();
  514. }
  515. }
  516. }
  517. }