IsolatedStorageFile.cs 20 KB

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