<UserControl x:Class="FileUploadSLApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Canvas Canvas.Left="10" Canvas.Top="10">
<Button Content="Upload" Height="20" Click="Button_Click_1" Margin="10,60,162,54" VerticalAlignment="Bottom" d:LayoutOverrides="Width, Height" HorizontalAlignment="Right" Background="#FF2770AF"></Button>
<TextBox Text="Write File Name with Extention" Margin="10,10,0,0" x:Name="txtFileName"></TextBox>
<Button Content="Read Eile" Height="20" Click="Button_Click" Margin="65,60,162,54" VerticalAlignment="Bottom" d:LayoutOverrides="Width, Height" HorizontalAlignment="Right" Background="#FF2770AF"></Button>
<Button Content="Get List" Height="20" Click="Button_Click_2" Margin="130,60,162,54" VerticalAlignment="Bottom" d:LayoutOverrides="Width, Height" HorizontalAlignment="Right" Background="#FF2770AF"></Button>
<Button x:Name="btnDelete" Height="20" Margin="190,60,251,8" Content="Delete File" Click="btnDelete_Click"/>
<Button x:Name="btnWrite" Height="20" Margin="270,60,36,8" Content="Write" Click="btnWrite_Click" />
<TextBlock Height="20" Margin="10,100,126,79" VerticalAlignment="Bottom" Text="Select File to uload" TextWrapping="Wrap" FontSize="16" x:Name="txtStatus"/>
<TextBox Text="Enter The File Contents here......" Margin="10,140,0,0" x:Name="txtContent"></TextBox>
</Canvas>
</Grid>
</UserControl>
private void btnWrite_Click(object sender, RoutedEventArgs e)
{
using (var MyAppStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!(MyAppStore.FileExists(txtFileName.Text)))
{
MessageBox.Show("File does not exist , we are creating one for you ");
IsolatedStorageFileStream file = MyAppStore.CreateFile(txtFileName.Text);
file.Close();
}
using (StreamWriter sw = new StreamWriter(MyAppStore.OpenFile(txtFileName.Text, FileMode.Open, FileAccess.Write)))
{
sw.WriteLine(txtContent.Text);
}
txtContent.Text = "";
txtFileName.Text = "";
MessageBox.Show("File Writen");
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//craeing a new link to the Application store for reading file in IsolatedStorage
using (var MyAppStore = IsolatedStorageFile.GetUserStoreForApplication())
{
//checking the existance of the file
if (!(MyAppStore.FileExists(txtFileName.Text)))
{
//if doesnot exist prompt the user
MessageBox.Show("File does not exist ");
//Clear the text from the textbox
txtFileName.Text = string.Empty;
}
//if file does exist
else
{
//create a stream reader to read the file
using (StreamReader sr = new StreamReader(MyAppStore.OpenFile(txtFileName.Text, FileMode.Open, FileAccess.ReadWrite)))
{
//read file till end and post the contenst in hte textblock
txtStatus.Text = sr.ReadToEnd();
}
}
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
//clearing the textblockstatus to ensure that it is not having any text
txtStatus.Text = string.Empty;
//getting the handler for the isolated store
using (var MyAppStore = IsolatedStorageFile.GetUserStoreForApplication())
{
//holding the array (list) of file names into a string array
string[] str = MyAppStore.GetFileNames();
//creating a foreach loop to go through each elemen of the array
foreach (string s in str)
{
//printing the filename in the textblock with a new line.
txtStatus.Text = txtStatus.Text + Environment.NewLine + s;
}
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
//creating the refrence for the dialog
OpenFileDialog dlg = new OpenFileDialog();
//creating the filter for the dialog
dlg.Filter = "All files (*.*)|*.*";
//Displaying the Dialog box remeber this should be user itnitated.
if (dlg.ShowDialog() == true)
{
txtStatus.Text = "Uploading Files ";
// Save all selected files into application's isolated storage
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
//holding the file name into a string variable for later use
string strname = dlg.File.Name;
//creating a filestream and reading the contents of the selected file
using (Stream fileStream = dlg.File.OpenRead())
{
//craeing a new IsolatedStorageFileStream for certaing new file in IsolatedStorage
using (IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream(strname, FileMode.Create, iso))
{
// Read and write the data block by block until finish
while (true)
{
//get the bytes to write in the file.
byte[] buffer = new byte[fileStream.Length];
// byte[] buffer = new byte[100001];
int count = fileStream.Read(buffer, 0, buffer.Length);
if (count > 0)
{
isoStream.Write(buffer, 0, count);
}
else
{
break;
}
}
}
txtStatus.Text = "Files Uploaded";
}
}
}
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
//getting the handler for the store
using (var MyAppStore = IsolatedStorageFile.GetUserStoreForApplication())
{
//Checking if file doesnot exist
if (!(MyAppStore.FileExists(txtFileName.Text)))
{
//prompt the user that file does not exist
MessageBox.Show("File does not exist ");
}
//if file does exist
else
{
//delete it from the store
MyAppStore.DeleteFile(txtFileName.Text);
//prompt the user that the file is deleted
MessageBox.Show("Deleted");
}
//clear the textbox containg the file name
txtFileName.Text = string.Empty;
}
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)