-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: Nasha
Message 1 in Discussion

 
Hey Group,  
To continue with our exploration on Isolated Storage Let us go ahead and create 
a sample application.  
1) Open a new Win App Place one listbox, one textbox and one button on the 
form.  
2) Our gaol is to serialize the contents of the listbox when the application 
shut downs and when the application runs it should load the serialized data 
back. IsolatedStorage works in a similar way as the file system . You can say 
it is a special type of file system which is specific to your application.Only 
your application knows where the files are stored.  
3) Getting started we will create a .txt file for our application in 
IsolationStorage which will hold all the serilized data. We first need to get 
the reference of the IsolatedStorage Store for out application. We do that by 
calling a static function of IsolatedStorageFile object called GetStore.  
4) Add code in the click event of the button to add the text entered in the 
textbox as an item in the listbox  
listBox2.Items.Add(textBox1.Text);
textBox1.Text = "";  
5) Since we have got reference to the store we can go ahead and create our 
textfile to store our data.We need to serialize all the items in the listbox. 
We will handle this in the Form Closing event.  
6) Get the reference to the Application Store by calling GetStore method on 
IsolatedStorageFile class  
IsolatedStorageFile isf = 
IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly | 
IsolatedStorageScope.User,null,null);  
If you check out all the other static methods of this object you will see that 
there are specific methonds to get Isolated Stores for User and Application 
Domain called as GetUserStoreFor Assembly and GetUserStoreForDomain. While 
calling this function remember to put a combination of two or more options for 
IsolatedStorageScope. This does not take a single value. Here we have given the 
scope as Assembly and User.  
7) Our second step will be to create a text file to hold our data. So we create 
a file named "IsolatedStoreProj.txt" using the IsolatedStorageFileStream 
object.  
IsolatedStorageFileStream isfs = new  
IsolatedStorageFileStream("IsolatedStoreProj.txt",System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.ReadWrite,System.IO.
  
FileShare.ReadWrite,isf);  
8) Now just loop through the items in the listbox and serialize all the data in 
the text file. After wrting the data in the file stream close the file 
stream.You complete code should look something like this :-  
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs 
e)
{
IsolatedStorageFile isf = 
IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly | 
IsolatedStorageScope.User,null,null);
IsolatedStorageFileStream isfs = new IsolatedStorageFileStream 
("IsolatedStoreProj.txt",System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.ReadWrite,System.IO.
 FileShare.ReadWrite,isf);
string [] sItems = new string[10];
listBox1.Items.CopyTo(sItems,0);
string oneString = null;
for(int i=0;i<sItems.GetLength(0);i++)
{
if(sItems[i] != null)
oneString += sItems[i].ToString() + ","; 
else 
break;
} 
oneString = oneString.Substring(0,oneString.Length - 1);
System.Text.ASCIIEncoding asc = new System.Text.ASCIIEncoding(); 
byte [] byteArray = asc.GetBytes(oneString);
isfs.Write(byteArray,0,byteArray.GetLength(0));
isfs.Close();
isf.Close();
}  
9) Compile and run this code. On exiting the application a text named 
IsolatedStoreProj.txt should be created. This file will be created in at the 
follwoing location  
<drive>:\Documents and Settings\<user>\Local Settings\Application 
Data\IsolatedStorage\<some folder>\<some 
folder>\AssemFiles\IsolatedStoreProj.txt  
10) Open the file and you will be able to view the contents we had serialized.  
11) Now let us load this data when our application starts up. We will read this 
file in the form_load event and populate our listbox.  
private void Form1_Load(object sender, System.EventArgs e)
{
IsolatedStorageFile isf = 
IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly | 
IsolatedStorageScope.User,null,null);
IsolatedStorageFileStream isfs = new IsolatedStorageFileStream 
("IsolatedStoreProj.txt",System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.Read,System.IO.FileShare.Read,isf);
byte[] byteArray = new byte[10000];
isfs.Read(byteArray,0,(int)isfs.Length);
System.Text.ASCIIEncoding asc = new System.Text.ASCIIEncoding(); 
string twoString = asc.GetString(byteArray);
string[] oneArray = twoString.Split(new char[]{','},10);
for(int i=0;i<oneArray.GetLength(0);i++)
listBox2.Items.Add(oneArray[i]);
isfs.Close();
isf.Close(); 
}  
12) In this way we can store our user setting , application related data in the 
Isolated store.  
13) This store that we have created in our sample application is a non-roaming 
store. To create a roaming store give the scope as IsolatedStorageScope.Roaming 
along with assembly and user as specified above.  
14) WinNT and Operationg systems after it have a feature called as roaming user 
profiles which allows the user to carry his perferences and presonal settings 
across machines on the network.On the same lines .Net provides us with roaming 
assembly stores so if the user has a roaming Windows profile his roaming 
assembly store will also roam.  
15) The assembly store is roaming only if it is mentioned explictly by 
mentioning the IsolatedStorageScope if it is not then the scope is non-roaming 
by default. Even if the user profile is roaming the isolated store can be 
non-roaming.  
16) You can create and list stores through code as well as use storeadm.exe for 
the same ( as per Part -1).  
  
-- Please post your queries and comments for my articles in the usergroup for 
the benefit of all. I hope this step from my end is helpful to all of us.  
Regards, 
Namratha (Nasha) 
  
  
 

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member 
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you 
received this message by mistake, please click the "Remove" link below. On the 
pre-addressed e-mail message that opens, simply click "Send". Your e-mail 
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to