Isolated Storage in .NET (Reading, Writing, and Deleting a File; Checking Available Space)


/ Published in: C#
Save to your folder(s)

Code taken from the video in the URL.


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.IO.IsolatedStorage;
  11.  
  12.  
  13. //'.NET Framework Developer's Guide
  14. //'Introduction to Isolated Storage
  15. //'http://msdn.microsoft.com/en-us/library/3ak841sy.aspx
  16.  
  17. //'Isolation by User and Assembly.
  18. //'Isolation by User, Domain, and Assembly.
  19. //'http://msdn.microsoft.com/en-us/library/eh5d60e1.aspx
  20.  
  21. //'Where depends on Operating System
  22. //'VISTA
  23. //'Roaming-enabled stores = <SYSTEMDRIVE>\Users\<user>\AppData\Roaming
  24. //'Nonroaming stores = <SYSTEMDRIVE>\Users\<user>\AppData\Local
  25. //'Pasted from <http://msdn.microsoft.com/en-us/library/3ak841sy.aspx>
  26.  
  27.  
  28.  
  29. namespace HDI_WinForms_IsolatedStorage
  30. {
  31. public partial class Form1 : Form
  32. {
  33. public Form1()
  34. {
  35. InitializeComponent();
  36. }
  37.  
  38. private void ButtonCreateFile_Click(object sender, EventArgs e)
  39. {
  40. IsolatedStorageFile isolatedStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
  41. IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.Append, FileAccess.Write, isolatedStore);
  42.  
  43. using (StreamWriter writer = new StreamWriter(isoStream))
  44. {
  45. writer.WriteLine("The Data " + DateTime.Now.ToShortTimeString());
  46. }
  47. }
  48.  
  49. private void ButtonReadIsoFile_Click(object sender, EventArgs e)
  50. {
  51. IsolatedStorageFile isolatedStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
  52. IsolatedStorageFileStream isolatedStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.Open, isolatedStore);
  53. using (StreamReader reader = new StreamReader(isolatedStream))
  54. {
  55. this.RichTextBox1.Text = reader.ReadToEnd();
  56. }
  57. }
  58.  
  59. private void ButtonSpaceAvailable_Click(object sender, EventArgs e)
  60. {
  61. IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain, null, null);
  62. ulong spaceAvailable = isolatedStorage.MaximumSize - isolatedStorage.CurrentSize;
  63. this.LabelSpaceAvailable.Text = spaceAvailable.ToString();
  64.  
  65. }
  66.  
  67. private void ButtonDeleteIsoFile_Click(object sender, EventArgs e)
  68. {
  69. IsolatedStorageFile isolatedStorage;
  70. isolatedStorage = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
  71. isolatedStorage.DeleteFile("TestStore.txt");
  72. }
  73. }
  74. }

URL: http://windowsclient.net/learn/video.aspx?v=90634

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.