Hi,
Please find below some sample code to read/write from/to the IsolatedStorage (note: other locations are not allowed due to web browser security restrictions).
1. Sample code using the IsolatedStorage class:
XAML code:
Code: Select all
<StackPanel>
<TextBox x:Name="TextBoxFileStorageDemo" Text="Enter some text"/>
<Button Content="Save to file using the IsolatedStorageFile class" Click="ButtonSaveToIsolatedStorageFile_Click"/>
<Button Content="Load from file using the IsolatedStorageFile class" Click="ButtonLoadFromIsolatedStorageFile_Click"/>
</StackPanel>
C# code:
Code: Select all
private void ButtonSaveToIsolatedStorageFile_Click(object sender, RoutedEventArgs e)
{
string fileName = "SampleFile.txt";
string data = TextBoxFileStorageDemo.Text;
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForAssembly())
{
IsolatedStorageFileStream fs = null;
using (fs = storage.CreateFile(fileName))
{
if (fs != null)
{
Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(data);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
MessageBox.Show("A new file named SampleFile.txt was successfully saved to the storage.");
}
else
MessageBox.Show("Unable to save the file SampleFile.txt to the storage.");
}
}
}
private void ButtonLoadFromIsolatedStorageFile_Click(object sender, RoutedEventArgs e)
{
string fileName = "SampleFile.txt";
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForAssembly())
{
if (storage.FileExists(fileName))
{
using (IsolatedStorageFileStream fs = storage.OpenFile(fileName, System.IO.FileMode.Open))
{
if (fs != null)
{
using (StreamReader sr = new StreamReader(fs))
{
string data = sr.ReadToEnd();
MessageBox.Show("The following text was read from the file SampleFile.txt located in the storage: " + data);
}
}
else
MessageBox.Show("Unable to load the file SampleFile.txt from the storage.");
}
}
else
MessageBox.Show("No file named SampleFile.txt was found in the storage.");
}
}
2. Sample code using the FileInfo class:
XAML code:
Code: Select all
<StackPanel>
<TextBox x:Name="TextBoxFileStorageDemo" Text="Enter some text"/>
<Button Content="Save to file using the FileInfo class" Click="ButtonSaveToFileInfo_Click"/>
<Button Content="Load from file using the FileInfo class" Click="ButtonLoadFromFileInfo_Click"/>
<Button Content="Delete file using the FileInfo class" Click="ButtonDeleteFileInfo_Click"/>
</StackPanel>
C# code:
Code: Select all
private void ButtonSaveToFileInfo_Click(object sender, RoutedEventArgs e)
{
string fileName = "Test.txt";
string data = TextBoxFileStorageDemo.Text;
FileInfo fileInfo = new FileInfo(fileName);
using (FileStream fs = fileInfo.OpenWrite())
{
Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(data);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
MessageBox.Show("A new file named Test.txt was successfully saved to the storage.");
}
}
private void ButtonLoadFromFileInfo_Click(object sender, RoutedEventArgs e)
{
string fileName = "Test.txt";
FileInfo fileInfo = new FileInfo(fileName);
if (fileInfo.Exists)
{
using (FileStream fs = fileInfo.OpenRead())
{
if (fs != null)
{
using (StreamReader sr = new StreamReader(fs))
{
string data = sr.ReadToEnd();
MessageBox.Show("The following text was read from the file Test.txt located in the storage: " + data);
}
}
else
MessageBox.Show("Unable to load the file Test.txt from the storage.");
}
}
else
MessageBox.Show("No file named Test.txt was found in the storage.");
}
private void ButtonDeleteFileInfo_Click(object sender, RoutedEventArgs e)
{
string fileName = "Test.txt";
FileInfo fileInfo = new FileInfo(fileName);
fileInfo.Delete();
MessageBox.Show("The file named Test.txt was successfully deleted from the local page storage.");
}
Regards,
JS-Support