using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.IO.IsolatedStorage; namespace IsolatedStorageDemo{ public class Form1 : System.Windows.Forms.Form{ #region "datamember" private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.Button button3; private System.ComponentModel.Container components = null; #endregion #region "ctor" public Form1(){ InitializeComponent();
} #endregion #region "dtor" protected override void Dispose( bool disposing ){ if( disposing ){ if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #endregion #region Vom Windows Form-Designer generierter Code /// <summary> /// Erforderliche Methode für die Designerunterstützung. /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden. /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.button3 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button1.Location = new System.Drawing.Point(16, 16); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(312, 23); this.button1.TabIndex = 0; this.button1.Text = "Isolated Storage File laden"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button2.Location = new System.Drawing.Point(16, 48); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(312, 23); this.button2.TabIndex = 1; this.button2.Text = "In Isolated Storage File schreiben"; this.button2.Click += new System.EventHandler(this.button2_Click); // // richTextBox1 // this.richTextBox1.Location = new System.Drawing.Point(16, 112); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(312, 232); this.richTextBox1.TabIndex = 2; this.richTextBox1.Text = ""; // // button3 // this.button3.FlatStyle = System.Windows.Forms.FlatStyle.System; this.button3.Location = new System.Drawing.Point(16, 80); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(312, 23); this.button3.TabIndex = 3; this.button3.Text = "Isolated Storage File löschen"; this.button3.Click += new System.EventHandler(this.button3_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(352, 358); this.Controls.Add(this.button3); this.Controls.Add(this.richTextBox1); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Isolated Storage Demo"; this.ResumeLayout(false);
} #endregion
[STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }
private void button1_Click(object sender, System.EventArgs e) { if ( this.CheckForIsoFile(this.GetIsolatedStorage()) ){ this.richTextBox1.Text = this.ReadFromIsoFile(this.GetIsolatedStorage()); } else{ MessageBox.Show("Konnte den Isolierten Speicherbereich nicht finden!"); } }
private IsolatedStorageFile GetIsolatedStorage(){ IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly , null, null); return isoStore; } private bool CheckForIsoFile(IsolatedStorageFile isoStore){ string[] fileNames = isoStore.GetFileNames("test.txt"); bool blfilefound = false; foreach (string file in fileNames){ if(file == "test.txt"){ blfilefound = true; } } return blfilefound; } private void WriteToIsoFile(IsolatedStorageFile isoStore, string inputvalue){ try{ StreamWriter writer = null;
if ( this.CheckForIsoFile( this.GetIsolatedStorage() ) ){ writer = new StreamWriter( new IsolatedStorageFileStream("test.txt", FileMode.Append, FileAccess.Write, isoStore)); } else{ writer = new StreamWriter(new IsolatedStorageFileStream("test.txt", FileMode.CreateNew,isoStore)); } writer.Write(inputvalue); writer.Close();
} catch ( Exception eXP ){ MessageBox.Show( eXP.Message ); } }
public string ReadFromIsoFile(IsolatedStorageFile isoStore){ System.Text.StringBuilder strBldResult; bool blfilefound = false; blfilefound = this.CheckForIsoFile(isoStore); if ( blfilefound ){ StreamReader reader = new StreamReader(new IsolatedStorageFileStream("test.txt", FileMode.Open,isoStore)); strBldResult = new System.Text.StringBuilder(); strBldResult.Append( reader.ReadToEnd() ); reader.Close(); return strBldResult.ToString(); } else{ return string.Empty; } }
private void button2_Click(object sender, System.EventArgs e) { this.WriteToIsoFile(this.GetIsolatedStorage(), this.richTextBox1.Text); }
private void button3_Click(object sender, System.EventArgs e) { try{ IsolatedStorageFile isoStore = this.GetIsolatedStorage(); if ( this.CheckForIsoFile(isoStore ) ){ isoStore.DeleteFile("test.txt"); } } catch ( Exception eXP ){ MessageBox.Show( eXP.Message ); } } }; } |