大家都知道在Silverlight中无法直接使用System.IO进行操作文件,当然这个是为了安全考虑,不过Silverlight提供了其他的操作方法,原理近似,同样很简单。
提到Silverlight中的文件操作,第一个肯定是独立存储Isolated Store,这个小编相当于于一个本地的小型存储空间,通过它可以把一些不重要的数据(用户的一些配置信息或者文件)
IsolatedStorageFile:
保存在客户端,由于这个空间是可以在本地查看得到,同时用户也可以随意的删除这些文件件以及文件,所以不要存放重要的信息。
IsolatedStorageFile.GetUserStoreForApplication();得到基于当前用户和当前应用程序的IsolatedStorageFile。
IsolatedStorageFile.GetUserStoreForSite();得到基于当前website域的所有IsolatedStorageFile(不止一个Application的,可能同一个website下有多个xap文件,那么这些是共享的),这样就可以在多个Application中共享信息。
下面先看下独立存储的规则:
1.不同的XAP文件,在同一个website下并且在同一个文件夹有不同的独立存储文件
2.如果Application在不同的site宿主,则有自己的独立存储文件
3.如果使用不同的TestPage,使用同一个XAP,则使用同一个独立存储文件
5.如果修改版本信息,等其他的程序及配置信息,则还使用同一个独立存储文件
6.如果替换一个名字一样的XAP文件,则还使用之前的独立存储文件
CreateDirectory() 创建一个文件夹在Isolated Store,根据用户指定的名字。
DeleteDirectory() 删除一个文件夹,根据用户指定的名字。
CreateFile() 创建一个文件,根据用户指定的名字,并且返回IsolatedStorageFileStream对象,可以用来进行写入操作。
Remove() 移除Isolated Store对象,包含所有的文件夹和文件。
OpenFile() 打开一个文件,并且返回IsolatedStorageFileStream对象。
FileExists() 判断一个文件是否存在,返回true或者false。
DirectoryExists() 判断一个文件夹是否存在,返回true或者false。
GetFileNames() 获得根目录(指定的目录)下的所有文件名称,返回一个string的array。
GetDirectoryNames() 获得根目录(指定目录下)的所有文件夹名称,返回一个string的array
使用IsolatedStorageFile读写数据:
StreamWriter和StreamReader用来读取和写入文本信息;
BinaryWriter和BinaryReader用来读取和写入二进制信息.
例子:
如果使用Windows vista或者Windows 7系统,则独立存储的文件在C:\Users\[UserName]\AppData\LocalLow\Microsoft\Silverlight\is目录。
当然在调试时候也可以看到,如下图:
创建文件:
IsolatedStorageFile storageFile=IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream myfs = storeFile.CreateFile(filePath);
using (StreamWriter mysw = new StreamWriter(myfs))
{
mysw.WriteLine(content);
}
通过使用IsolatedStorageFile.CreateFile创建一个文件,得到返回的IsolatedStorageFileStream对象,使用该对象创建StreamWriter对象调用WriteLine方法写入一行数据。
读取文件:
//使用这个方法合并目录和文件路径
string path = System.IO.Path.Combine(this.txtDirectionName.Text,this.txtFileName.Text);
判断是否存在这个文件if (storageFile.FileExists(path))
{
创建一个读取流,参数为OpenIFile方法读取指定位置文件的流
StreamReader sw = new StreamReader(storageFile.OpenFile(path,FileMode.Open,FileAccess.Read));
读取文件内容
this.txtFileContent.Text = sw.ReadToEnd();
}
首先判断是否存在该文件,如果存在则调用IsolatedStorageFile.OpenFile函数返回一个IsolatedStorageFileStream对象,使用该对象创建StreamReader对象调用ReadToEnd
函数得到文本信息。
IsolatedStorageFile storageFile=IsolatedStorageFile.GetUserStoreForApplication();
storageFile.DeleteFile(path);
创建目录:
IsolatedStorageFile storageFile=IsolatedStorageFile.GetUserStoreForApplication();
storageFile.CreateDirectory(path);
获得列目录(即获得指定目录或者整个应用程序的目录):
IsolatedStorageFile storageFile=IsolatedStorageFile.GetUserStoreForApplication();
storageFile.GetDirectoryNames("*");当前的*表示获得所有的目录
删除目录:
IsolatedStorageFile storageFile=IsolatedStorageFile.GetUserStoreForApplication();
storageFile.DeleteDirectory(path);
添加空间:
默认的存储空间是1MB,当程序以OOB模式运行时候,则会增加到25MB,不管是 OOB还是Web模式,都是使用同一个独立存储。
如果要增加存储空间,则调用IsolatedStorageFile.IncreaseQuotaTo()来增加到想要的空间大小,这里是按字节来算。
使用该方法需要注意两点:
使用SolatedStorageFile.Quota得到当前独立存储的空间大小。
使用IsolatedStorageFile.AvailableFreeSpace得到当前可用的独立存储空间大小。
通过XmlSerializer存储Object对象:
其实这个功能还是很好用的,也是很有用,通过XmlSerializer将对象进行序列化保存在文件中,当我们需要的时候再通过XmlSerializer进行反序列化得到使用的对象。
XmlSerializer通过转换字节流对象来实现整个的序列化和反序列化,是可以针对任何Stream的。
使用XmlSerializer步骤如下:
1.添加System.Xml.Serializtion.dll
3.类需要有公有的setter属性构成,当XmlSerializer进行序列化和反序列化的时候则会使用到这些属性,则忽略私有的字段
下面来一个完整的例子:
User实体类:
public class User
{
string FirstName { get; set; }
string LastName { public DateTime? DateOfBirth { public User(string firstName,255)">string lastName,DateTime? dateOfBirth)
{
FirstName = firstName;
LastName = lastName;
DateOfBirth = dateOfBirth;
}
public User() { }
}
XAML代码: Grid x:Name =LayoutRoot Background =White Grid.ColumnDeFinitions ColumnDeFinition / ColumnDeFinition ColumnDeFinition / ColumnDeFinition / Grid.ColumnDeFinitions StackPanel Grid.Column =0 Lservice端代码: /// summary /// 获取文件列表 /// /summary /// returns/returns [WebMethod] public string [] GetFileList() { // 得到指定目录下的所有文件列表 string [] files = Directory.GetFiles(FilePatXAML代码:
<Grid x:Name="LayoutRoot" Background="White">
Grid.ColumnDeFinitionsColumnDeFinition></</StackPanel Grid.Column="0"ListBox ="lstUsers"ListBoxButton Height="25" Content="Delete" x:Name="btnDelete" Click="btnDelete_Click"ButtonStackPanel="1"GridGrid.RowDeFinitionsRowDeFinitionsdk:Label ="lblFirstName"="FirstName" Grid.Row="0" Grid.Column="0" />
TextBox ="txtFirstName"="1" ="lblLastName"="LastName"="1"="txtLastName"="lblDateOfBirth"="Date Of Birth"="2"sdk:DatePicker ="dateBirth" Grid.Row="Add or Update"="btnModify"="btnModify_Click"="3">效果图如下:
左边一个ListBox显示所有的FileName,可以进行删除,当选择一个ListItem项 则在右边显示对应的对象的信息.
下面是Xaml.cs代码:
partial class IsolatedStoreByXmlSerializer : UserControl
{
public IsolatedStoreByXmlSerializer()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(IsolatedStoreByXmlSerializer_Loaded);
this.lstUsers.SelectionChanged += new SelectionChangedEventHandler(lstUsers_SelectionChanged);
}
void lstUsers_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
得到选中的项对应的File
using (StreamReader sr = new StreamReader(store.OpenFile(lstUsers.SelectedItem.ToString(),FileAccess.Read)))
{
进行反序列化操作
User user = (User)serializer.Deserialize(sr);
txtFirstName.Text = user.FirstName;
txtLastName.Text = user.LastName;
dateBirth.SelectedDate = user.DateOfBirth;
}
}
}
定义序列化对象,当前类型为User
private XmlSerializer serializer = new XmlSerializer(typeof(User));
void IsolatedStoreByXmlSerializer_Loaded(ottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,RoutedEventArgs e)
{
得到所有的后缀为.user的文件
lstUsers.ItemsSource = store.GetFileNames(*.user");
}
}
private void btnModify_Click(实例化对象,值为文本输入的值
User user = new User() { FirstName = txtFirstName.Text,LastName = txtLastName.Text,DateOfBirth =dateBirth.SelectedDate};
得到创建文件的流using (StreamWriter sw = new StreamWriter(store.CreateFile(user.FirstName + user.LastName + .user")))
{
进行序列化操作
serializer.Serialize(sw,user);
}
lstUsers.ItemsSource = store.GetFileNames(");
}
}
void btnDelete_Click(如果选择的项不为空if (lstUsers.SelectedItem != null)
{
删除选中的文件
store.DeleteFile(lstUsers.SelectedItem.ToString());
lstUsers.ItemsSource = store.GetFileNames(");
}
}
}
}在代码中实现的功能是,在右边可以进行新增一个File文件,在左边显示完整的File的列表,当选中一项会在右边显示其对应的详细信息,当然也可以进行删除操作。
IsolatedStorageSettings :
可以看到在上边中使用IsolatedStorageFile多用来操作文档等文件,在Silverlight中还提供了一个很有用的对象IsolatedStorageSettings,可以用来保存一些信息,这些信息是
直观的,也就是可以直接拿来使用,该对象是一个键对值的形式存在,该对象提供了两个属性ApplicationSettings和SiteSettings,同样的前者表示当前Application的存储数据,
后者表示当前website域的存储数据。这个对象有点类似于Cookie的形式在存放,当你关闭网页,或者是重新登录,这些信息还都是存在的。
例子:
IsolatedStorageSettings userSettings = IsolatedStorageSettings.SiteSettings;
userSettings[fly"] = ";
userSettings[user"] = new User() { FirstName=wang",LastName=Flyottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(128,DateOfBirth=DateTime.Now };
User user = (User)userSettings["];可以看到,Setting是不仅可以存放简单的数据,也可以存放复杂的类型。
OpenFileDialog:
没错,就是这个OpenFileDialog,和之前的使用方法一摸一样,同样是先打开选择窗体,选择文件,点击确定,处理文件。
OpenFileDialog dialog = new OpenFileDialog();
实例化OpenFileDialog;
指定Dialog的筛选器类型字符串:
dialog.Filter = "Text Files (*.txt)|*.txt";
选择不同类型的文件:
dialog.Filter = "Bitmaps (*.bmp)|*.bmp|JPEGs (*.jpg)|*.jpg|All files (*.*)|*.*";
或者:
dialog.Filter = "Image Files (*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif";
通常来说处理读取分为两类,文本和非文本,文本很简单直接使用OpenFileDialog.OpenText();如果是二进制的则需要使用OpenFileDialog.OpenRead()。
读取文本:
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = Text Files (*.txt)*.txt";
if (fileDialog.ShowDialog() == true)
{
using (StreamReader sr = fileDialog.File.OpenText())
{
lblMsg.Text = sr.ReadToEnd();
}
}是不是很简单呢。
读取非文本(当前例子是Image):
OpenFileDialog fileDialog = Images Files (*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gifusing (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (Stream fileStream = fileDialog.File.OpenRead())
{
如果当前的文件超过了可使用的空间,则增加空间
if (fileStream.Length > store.AvailableFreeSpace)
{
store.IncreaseQuotaTo(fileStream.Length);
}
using (IsolatedStorageFileStream storeStraem=store.CreateFile(fileDialog.File.Name))
{
byte [] buffer=new byte[1024];
int count = 0;
do
{
count = fileStream.Read(buffer,0,buffer.Length);
if (count > 0) storeStraem.Write(buffer,buffer.Length);
} while (count>0);
}
}
}
}首先使用OpenFileDialog.File.OpenRead()得到Stream流,判断当前流的长度和独立存储的可用空间大小,如果大于独立存储则增加独立存储空间;
使用IsolatedStorageFile.CreateFile创建文件并且得到IsolatedStorageFileStream对象;然后定了一个byte的数组用于读取文件流中的数据,暂且为1024字节长度,通过Stream.Read方法判断当前是否还存在未读取的数据,如果存在则使用IsolatedStorageFileStream.Write向文件中写入数据,并且使用循环来实现。
并且OpenFileDialog支持多选,设置OpenFileDialog.Multiselect为ture则可实现多选,为false禁止多选,多选后使用OpenFileDialog.Files属性得到。
修改如下:
OpenFileDialog fileDialog = 遍历Files,得到FileInfo对象
foreach (FileInfo file in fileDialog.Files)
{
using (Stream fileStream = file.OpenRead())
{
if (fileStream.Length > store.AvailableFreeSpace)
{
store.IncreaseQuotaTo(fileStream.Length);
}
using (IsolatedStorageFileStream storeStraem = store.CreateFile(fileDialog.File.Name))
{
byte[] buffer = do
{
count = fileStream.Read(buffer,buffer.Length);
} while (count > 0);
}
}
}
}
}
service端代码:
/// <summary>
/// 获取文件列表
</summary><returns></returns>
[WebMethod]
string[] GetFileList()
{
得到指定目录下的所有文件列表
string[] files = Directory.GetFiles(FilePath);
for (int i = 0; i < files.Length; i++)
{
得到文件的名称
files[i] = Path.GetFileName(files[i]);
}
return files;
}
下载文件
<param name="fileName">文件名称</param>byte[] DownloadFile(string fileName)
{
string file = Path.Combine(FilePath,fileName);
using (FileStream myfs=new FileStream (file,FileMode.Open))
{
byte[] bytes=byte[myfs.Length];
myfs.Read(bytes,bytes.Length);
return bytes;
}
}
上传文件
<param name="bytes">文件字节数组</param>void UploadFile(string fileName,255)">byte[] bytes)
{
ottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,FileMode.Create))
{
myfs.Write(bytes,bytes.Length);
}
}
看下前台的操作:
="lstFile" Height="200"Orientation="Horizontal"="btnDownload"="btnDownload_Click"="DownLoad" Width="200" Margin="5"="btnUpload"="btnUpload_Click"="Upload">
就是一个StackPanel然后一个ListBox显示文件名称列表,两个按钮一个是上传一个是下载。
class OperateFileByService : UserControl
{
public OperateFileByService()
{
InitializeComponent();
new RoutedEventHandler(OperateFileByService_Loaded);
}
private FileServiceSoapClient proxy = new FileServiceSoapClient();
void OperateFileByService_Loaded(ottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,RoutedEventArgs e)
{
proxy.GetFileListCompleted += new EventHandler<GetFileListCompletedEventArgs>(proxy_GetFileListCompleted);
proxy.GetFileListAsync();
proxy.CloseAsync();
}
void proxy_GetFileListCompleted(ottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,GetFileListCompletedEventArgs e)
{
lstFile.ItemsSource = e.Result;
}
void btnDownload_Click(if (lstFile.Selectedindex !=-1)
{
string fileName = lstFile.SelectedItem.ToString();
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "jpg";if (saveDialog.ShowDialog() == true)
{
proxy.DownloadFileCompleted += new EventHandler<DownloadFileCompletedEventArgs>(proxy_DownloadFileCompleted);
proxy.DownloadFileAsync(fileName,saveDialog);
}
}
}
void proxy_DownloadFileCompleted(ottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,DownloadFileCompletedEventArgs e)
{
if (e.Error == null)
{
得到下载的字节数据数组byte[] data = e.Result;
SaveFileDialog saveDialog = (SaveFileDialog)e.UserState;
using (Stream fs = saveDialog.OpenFile())
{
fs.Write(data,data.Length);
}
}
}
void btnUpload_Click(ottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,RoutedEventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
if (fileDialog.ShowDialog()==try
{
using (Stream stream= fileDialog.File.OpenRead())
{
if (stream.Length < 5120000)
{
byte[] data = byte[stream.Length];
stream.Read(data,data.Length);
proxy.UploadFileCompleted +=new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_UploadFileCompleted);
proxy.UploadFileAsync(fileDialog.File.Name,data);
}
else
{
MessageBox.Show(文件大于5MB");
}
}
}
catch (Exception)
{
throw;
}
}
}
void proxy_UploadFileCompleted(ottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; color:rgb(0,System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error==null)
{
proxy.GetFileListAsync();
}
}
}
以上就是对文件的操作总结,希望大家多给意见。
本文来自wangyafei_it的博客,原文地址:http://www.cnblogs.com/ListenFly/archive/2012/01/31/2311289.html
(1存取小数据)
private void btnSaveSetting_Click(object sender,RoutedEventArgs e){IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;// txtInput is a TextBox defined in XAML.if (!settings.Contains("userData")){settings.Add("userData",txtInput.Text);}else{settings["userData"] = txtInput.Text;}settings.Save();}
private void btndisplaySetting_Click(object sender,RoutedEventArgs e){// txtdisplay is a TextBlock defined in XAML.txtdisplay.Text = "USER DATA: ";if (IsolatedStorageSettings.ApplicationSettings.Contains("userData")){txtdisplay.Text +=IsolatedStorageSettings.ApplicationSettings["userData"] as string;}}
(2读取图片)
private PhotochooserTask photoTask;// Constructorpublic MainPage(){InitializeComponent();photoTask = new PhotochooserTask();photoTask.Completed += new EventHandler<PhotoResult>(photoTask_Completed);}void photoTask_Completed(object sender,PhotoResult e){if (e.TaskResult == TaskResult.OK){BitmapImage bmp = new BitmapImage();bmp.SetSource(e.ChosenPhoto);SavetoFile(bmp);}}private void btnSaveFile_Click(object sender,RoutedEventArgs e){photoTask.Show();}private void SavetoFile(BitmapImage image){using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()){WriteableBitmap bmImage = new WriteableBitmap(image);if (!store.DirectoryExists("Images")){store.CreateDirectory("Images");}using (IsolatedStorageFileStream isoStream =store.OpenFile(@"Images\userPhoto.jpg",FileMode.OpenorCreate)){Extensions.SaveJpeg(bmImage,isoStream,bmImage.PixelWidth,bmImage.PixelHeight,0,100);}}}(3读取路径下文件)private BitmapImage GetSavedImage(){BitmapImage image;using (IsolatedStorageFile store =IsolatedStorageFile.GetUserStoreForApplication()){using (IsolatedStorageFileStream isoStream =store.OpenFile(@"Images\userPhoto.jpg",FileMode.Open)){image = new BitmapImage();image.SetSource(isoStream);}}return image;}private void btndisplayFile_Click(object sender,RoutedEventArgs e){BitmapImage bitmap = GetSavedImage();if (bitmap != null){// userImage is defined in MainPage.xamluserImage.source = bitmap;}}