24 Haziran 2012 Pazar

Isolated Storage – Dosya Ve Klasor Islemleri


Bu makalemde Isolated Storage’den biraz bahsettikten sonra hemen ardından dosya ve dizin islemleri ile ilgili geniş kapsamlı bir uygulama yapıyor olacağız.
Öncelikle Isolated Storage hakkında biraz bilgi verecek olursak; uygulamalarınızda izole olarak sadece bir uygulamanın kendisine has bir şekilde kullanabileceği ve veri saklayabilmesi için faydalanabileceği bir alan olarak kullanılmaktadır.
Uygulama içerisindeyken verilerinizi kaydederek daha sonrasında yine uygulama içerisindeyken sakladığınız bu verileri tekrar kullanmak için çekebilirsiniz. Peki nerede kullanabilirim bu özelliği diye düşünecek olursak; uygulamanız çalışırken telefonunuzdan arandığınızda, o an için belleğinde bulunan verileri kaydetmek istemeyebilirsiniz. Bu durumda ayarlarınızı ve kullanıcı bilgilerinizin bulunduğu dosyaları kaydetmek için Isolated Storage tüm ihtiyaçlarınızı karşılayacaktır.


Isolated Storage kullanımında uygulamanıza using System.IO.IsolatedStorage; ifadesini eklemelisiniz.
Şimdi dilerseniz uygulamamıza geçelim, uygulamamızda dosya ismini, klasör ismini ve dosyaya kaydolucak verileri kullanıcıdan alıp hemen aşağısında kaydını yaparken nasıl bir işlem yapması gerektiğini seçtiriyoruz.
Kullanıcı girdiği bu bilgileri ya Root’a ya da klasöre kaydedebilecektir.
Kullanıdığımız nesneler hakkında ki bilgilerde şöyle olacaktır;
Button1 in click olayında bir metin dosyasına metin yazdırmak için kullanıyoruz.
Button2 nin click olayı ile birlikte oluşturduğumuz text dosyasını okuyabiliyoruz.
Button5 in click olayı ile birlikte oluşturduğumuz text dosyasını silebiliyoruz.
Button3 ün click olayı ile birlikte klasör içerisinde ki bir metin dosyasına yeni bir metin dosyası oluşturuyoruz.
Button4 ün click olayı ile birlikte ise klasör içerisinde ki metin dosyalarını okuyabiliriz.

Xaml Tasarım Ekrani ;

















<phone:PhoneApplicationPage
x:Class=”File_And_Folder_App.MainPage”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:phone=”clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone”
xmlns:shell=”clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone”
xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″
xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″
mc:Ignorable=”d” d:DesignWidth=”480″ d:DesignHeight=”768″
FontFamily=”{StaticResource PhoneFontFamilyNormal}”
FontSize=”{StaticResource PhoneFontSizeNormal}”
Foreground=”{StaticResource PhoneForegroundBrush}”
SupportedOrientations=”Portrait” Orientation=”Portrait”
shell:SystemTray.IsVisible=”True”>
<Grid x:Name=”LayoutRoot” Background=”Transparent”>
<Grid.RowDefinitions>
<RowDefinition Height=”Auto”/>
<RowDefinition Height=”*”/>
</Grid.RowDefinitions>
<StackPanel x:Name=”TitlePanel” Grid.Row=”0″ Margin=”12,17,0,28″>
<TextBlock x:Name=”ApplicationTitle” Text=”Dosya ve Klasör İşlemleri Uygulaması” Style=”{StaticResource PhoneTextNormalStyle}”/>
<TextBlock x:Name=”PageTitle” Text=”Dosya / Klasör” Margin=”9,-7,0,0″ Style=”{StaticResource PhoneTextTitle1Style}”/>
</StackPanel>
<Grid x:Name=”ContentPanel” Grid.Row=”1″ Margin=”12,0,12,0″>
<TextBlock Height=”30″ HorizontalAlignment=”Left” Margin=”15,41,0,0″ Name=”textBlock1″ Text=”Dosya Adı” VerticalAlignment=”Top” />
<TextBox Height=”72″ HorizontalAlignment=”Left” Margin=”132,19,0,0″ Name=”txtDosyaIsmi” Text=”" VerticalAlignment=”Top” Width=”304″ />
<TextBlock Height=”30″ HorizontalAlignment=”Left” Margin=”15,105,0,0″ Name=”textBlock2″ Text=”Klasör Adı” VerticalAlignment=”Top” />
<TextBox Height=”72″ HorizontalAlignment=”Left” Margin=”132,83,0,0″ Name=”txtKlasorIsmi” Text=”" VerticalAlignment=”Top” Width=”304″ />
<TextBlock Height=”30″ HorizontalAlignment=”Left” Margin=”15,168,0,0″ Name=”textBlock3″ Text=”Veriyi Giriniz” VerticalAlignment=”Top” />
<TextBox Height=”149″ HorizontalAlignment=”Left” Margin=”132,146,0,0″ Name=”txtVeriAl” Text=”" VerticalAlignment=”Top” Width=”304″ />
<Button Content=”Kaydet” Height=”72″ HorizontalAlignment=”Left” Margin=”9,357,0,0″ Name=”button1″ VerticalAlignment=”Top” Width=”220″ Click=”button1_Click” />
<CheckBox Content=”Root’a Kaydet” Height=”72″ HorizontalAlignment=”Left” Margin=”-1,289,0,0″ Name=”cbroot” VerticalAlignment=”Top” />
<CheckBox Content=”Klasore Kaydet” Height=”72″ HorizontalAlignment=”Right” Margin=”0,289,45,0″ Name=”cbfolder” VerticalAlignment=”Top” />
<Button Content=”Oku” Height=”72″ HorizontalAlignment=”Left” Margin=”222,357,0,0″ Name=”button2″ VerticalAlignment=”Top” Width=”211″ Click=”button2_Click” />
<Button Content=”Dizine Yaz” Height=”72″ HorizontalAlignment=”Left” Margin=”12,418,0,0″ Name=”button3″ VerticalAlignment=”Top” Width=”421″
Click=”button3_Click” />
<Button Content=”Dizinden Oku” Height=”72″ HorizontalAlignment=”Left” Margin=”12,478,0,0″ Name=”button4″ VerticalAlignment=”Top” Width=”421″ Click=”button4_Click” />
<Button Content=”Dosyayı Kalıcı Olarak Sil” Height=”72″ HorizontalAlignment=”Left” Margin=”15,537,0,0″ Name=”button5″ VerticalAlignment=”Top” Width=”421″ Click=”button5_Click” />
</Grid>
</Grid>
</phone:PhoneApplicationPage>

MainPage.xaml.Cs Kaynak Kodları ;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.IO.IsolatedStorage;
namespace File_And_Folder_App
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (cbroot.IsChecked == true)
{
string strDosyaIsmi = txtDosyaIsmi.Text.ToString();
string strVeriAl = txtVeriAl.Text.ToString();
IsolatedStorageFile IS_File = IsolatedStorageFile.GetUserStoreForApplication();
using (StreamWriter SW_File = new StreamWriter(new IsolatedStorageFileStream(strDosyaIsmi, FileMode.Create, FileAccess.Write, IS_File)))
{
SW_File.WriteLine(strVeriAl);
SW_File.Close();
}
MessageBox.Show(“Dosya Kaydedildi…”);
}
else
{
MessageBox.Show(“Dosyanızı kök dizin veya bir klasor uzerine kaydetmeyi seçtiniz.”);
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
string strDosyaIsmi = txtDosyaIsmi.Text.ToString();
IsolatedStorageFile IS_File = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream IS_FileStream = IS_File.OpenFile(strDosyaIsmi, FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(IS_FileStream))
{
string strVeri = reader.ReadLine();
MessageBox.Show(strVeri.ToString());
}
}
private void button5_Click(object sender, RoutedEventArgs e)
{
string strDosyaIsmi = txtDosyaIsmi.Text.ToString();
IsolatedStorageFile IS_File = IsolatedStorageFile.GetUserStoreForApplication();
IS_File.DeleteFile(strDosyaIsmi);
}
private void button3_Click(object sender, RoutedEventArgs e)
{
if (cbfolder.IsChecked == true)
{
string strDosyaIsmi = txtDosyaIsmi.Text.ToString();
string strKlasorIsmi = txtKlasorIsmi.Text.ToString();
string strVeriAl = txtVeriAl.Text.ToString();
IsolatedStorageFile IS_File = IsolatedStorageFile.GetUserStoreForApplication();
IS_File.CreateDirectory(strKlasorIsmi);
string strVeriYolu = strKlasorIsmi + “\\” + strDosyaIsmi;
StreamWriter swWriter = new StreamWriter(new IsolatedStorageFileStream(strVeriYolu, FileMode.OpenOrCreate, IS_File));
swWriter.WriteLine(strVeriAl);
swWriter.Close();
MessageBox.Show(“Dosya Kaydedildi…”);
}
else
{
MessageBox.Show(“Dosyanızı kök dizin veya bir klasor uzerine kaydetmeyi seçtiniz.”);
}
}
private void button4_Click(object sender, RoutedEventArgs e)
{
string strDosyaIsmi = txtDosyaIsmi.Text.ToString();
string strKlasorIsmi = txtKlasorIsmi.Text.ToString();
string strVeriYolu = strKlasorIsmi + “\\” + strDosyaIsmi;
IsolatedStorageFile IS_File = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream IS_FileStream = IS_File.OpenFile(strVeriYolu, FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(IS_FileStream))
{
string strVeri = reader.ReadLine();
MessageBox.Show(strVeri.ToString());
}
}
}
}

Uygulamanın Ekran Goruntusu ;

Uygulamamızı çalıştırıyoruz,














Aşağıda görüldüğü gibi kullanıcıdan verileri almaktadır.














Keyifli Çalışmalar Dilerim…

1 yorum:

  1. Güzel çalışma peki Isolated Storage le xml dosyası oluşturup oluşturulan xml deki attribute lerini nasıl güncelleyebilriz?ben XML i oluşturdum element attribute ekledim fakat xml deki attribute yi güncelleyemedim.

    YanıtlaSil