Windows 8技巧:关于windows 8的文件管理 File创建和String Stream Buffer方式读写的
在本文中我们将学习Windows 8中的文件创建和多种读写方式以及设置文档库访问权限和文件类型的访问。
当然我们需要做以下准备工作:
首先:设置程序允许访问的文件位置为:"库\文档",设置方法:点击”Package.appxmanifest“,然后选择”功能“选项卡,在功能列表中勾选”文档库访问“。如下图:
其次:设置程序允许以上文件夹内的文件类型,本实例中设置为txt后缀的文件:设置方法:点击”Package.appxmanifest“,然后选择”声明“选项卡,在可用声明下拉列表中选择”文件类型关联“,点击”添加“按钮,并且在右边的列表中添加”支持的文件类型为.txt,并且设置名称为txt,当然你也可以继续添加允许访问dat文件等,如下图:
准备工作做好了,我们需要创建一个项目,然后写入一下代码进行访问文件以及文件夹,创建文件和读写文件。
Xaml代码:
复制代码代码如下:
Name="btnCreateFile" VerticalAlignment="Top" Click="btnCreateFile_Click"/>
x:Name="btnWriteFile_Copy" VerticalAlignment="Top" Click="btnWriteFile_Copy_Click"/>
x:Name="btnReadFile" VerticalAlignment="Top" Click="btnReadFile_Click"/>
x:Name="btnWriteBufferFile" VerticalAlignment="Top" Click="btnWriteBufferFile_Click" />
x:Name="btnReadBufferFile" VerticalAlignment="Top" Click="btnReadBufferFile_Click"/>
x:Name="btnWriteStreamFile" VerticalAlignment="Top" Click="btnWriteStreamFile_Click" />
x:Name="btnReadStreamFile" VerticalAlignment="Top" Click="btnReadStreamFile_Click"/>
cs代码:
复制代码代码如下:
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
///
/// Invoked when this page is about to be displayed in a Frame.
///
/// Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
//获取“库\文档”文件夹
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
//创建文件以及普通string写入读取文本
private async void btnCreateFile_Click(object sender, RoutedEventArgs e)
{
StorageFile sf= await storageFolder.CreateFileAsync(this.tbFileName.Text.Trim(),
CreationCollisionOption.ReplaceExisting);
tb_show.Text = "提示:创建了文件--" + this.tbFileName.Text.Trim();
}
private async void btnWriteFile_Copy_Click(object sender, RoutedEventArgs e)
{
try
{
string writestr = this.tbContent.Text.Trim() + "text方式";
StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
await FileIO.WriteTextAsync(sf, writestr);
tb_show.Text = "提示:文件写入成功,写入内容为-“" + writestr + "”";
}
catch (Exception ex)
{
tb_show.Text = "提示:未找到该文件,请先创建文件";
}
}
private async void btnReadFile_Click(object sender, RoutedEventArgs e)
{
try
{
StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
string filecontent= await FileIO.ReadTextAsync(sf,UnicodeEncoding.Utf8);
tb_show.Text = "提示:文件以string方式读取成功,读取的内容为-“" + filecontent+"”";
}
catch (Exception ex)
{
tb_show.Text = "提示:未找到该文件,请先创建文件";
}
}
//Buffer方式写入和读取
private async void btnWriteBufferFile_Click(object sender, RoutedEventArgs e)
{
try
{
string writestr = this.tbContent.Text.Trim() + "buffer方式";
StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
IBuffer buffer = GetBufferFromString(writestr);
await FileIO.WriteBufferAsync(sf, buffer);
tb_show.Text = "提示:文件写入成功,写入内容为-“" + writestr + "”";
}
catch (Exception ex)
{
tb_show.Text = "提示:未找到该文件,请先创建文件";
}
}
private async void btnReadBufferFile_Click(object sender, RoutedEventArgs e)
{
StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
IBuffer buffer = await FileIO.ReadBufferAsync(sf);
using (DataReader dataReader = DataReader.FromBuffer(buffer))
{
string filecontent = dataReader.ReadString(buffer.Length);
tb_show.Text = "提示:文件以Buffer方式读取成功,读取的内容为-“" + filecontent + "”";
}
}
//将String转为Buffer
private IBuffer GetBufferFromString(String str)
{
using (InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream())
{
using (DataWriter dataWriter = new DataWriter(memoryStream))
{
dataWriter.WriteString(str);
return dataWriter.DetachBuffer();
}
}
}
//Stream方式写入和读取
private async void btnWriteStreamFile_Click(object sender, RoutedEventArgs e)
{
try
{
string writestr = this.tbContent.Text.Trim() + "Stream方式";
StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
using (StorageStreamTransaction transaction = await sf.OpenTransactedWriteAsync())
{
using (DataWriter dataWriter = new DataWriter(transaction.Stream))
{
dataWriter.WriteString(writestr);
transaction.Stream.Size = await dataWriter.StoreAsync(); // reset stream size to override the file
await transaction.CommitAsync();
tb_show.Text = "提示:文件写入成功,写入内容为-“" + writestr + "”";
}
}
}
catch (Exception ex)
{
tb_show.Text = "提示:未找到该文件,请先创建文件";
}
}
private async void btnReadStreamFile_Click(object sender, RoutedEventArgs e)
{
StorageFile sf = await storageFolder.GetFileAsync(this.tbFileName.Text.Trim());
using (IRandomAccessStream readStream = await sf.OpenAsync(FileAccessMode.Read))
{
using (DataReader dataReader = new DataReader(readStream))
{
UInt64 size = readStream.Size;
if (size <= UInt32.MaxValue)
{
UInt32 numBytesLoaded = await dataReader.LoadAsync((UInt32)size);
string filecontent = dataReader.ReadString(numBytesLoaded);
tb_show.Text = "提示:文件以Stream方式读取成功,读取的内容为-“" + filecontent + "”";
}
}
}
}
}
如需源码请点击 Win8File_jb51net.rar 下载。VS2012+Windows8开发。
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
本文地址:/jtjc/win10/167964.html