在本文中我们将学习Windows 8中的文件创建和多种读写方式以及设置文档库访问权限和文件类型的访问。
当然我们需要做以下准备工作:
首先:设置程序允许访问的文件位置为:"库\文档",设置方法:点击”Package.appxmanifest“,然后选择”功能“选项卡,在功能列表中勾选”文档库访问“。如下图:
其次:设置程序允许以上文件夹内的文件类型,本实例中设置为txt后缀的文件:设置方法:点击”Package.appxmanifest“,然后选择”声明“选项卡,在可用声明下拉列表中选择”文件类型关联“,点击”添加“按钮,并且在右边的列表中添加”支持的文件类型为.txt,并且设置名称为txt,当然你也可以继续添加允许访问dat文件等,如下图:
准备工作做好了,我们需要创建一个项目,然后写入一下代码进行访问文件以及文件夹,创建文件和读写文件。
Xaml代码:
复制代码代码如下:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<!--显示区-->
<TextBlock HorizontalAlignment="Left" Margin="137,42,0,0" TextWrapping="Wrap" Text="文件名:"
VerticalAlignment="Top" Height="23" Width="43"/>
<TextBox HorizontalAlignment="Left" Margin="185,33,0,0" TextWrapping="Wrap"
Text="test.txt" VerticalAlignment="Top" Width="121" Name="tbFileName"/>
<TextBox HorizontalAlignment="Left" Margin="457,33,0,0" TextWrapping="Wrap"
Text="默认需要添加的文件内容" VerticalAlignment="Top" Width="431" Name="tbContent"/>
<TextBlock HorizontalAlignment="Left" Margin="396,42,0,0" TextWrapping="Wrap" Text="文件内容:"
VerticalAlignment="Top" Height="23" Width="61"/>
<TextBlock HorizontalAlignment="Left" Margin="127,317,0,0" TextWrapping="Wrap" Text="提示:"
VerticalAlignment="Top" Height="23" Width="761" Name="tb_show"/>
<!--创建文件以及普通string写入读取文本-->
<Button Content="创建文件" HorizontalAlignment="Left" Margin="127,99,0,0"
Name="btnCreateFile" VerticalAlignment="Top" Click="btnCreateFile_Click"/>
<Button Content="写入string文件" HorizontalAlignment="Left" Margin="430,99,0,0"
x:Name="btnWriteFile_Copy" VerticalAlignment="Top" Click="btnWriteFile_Copy_Click"/>
<Button Content="读取string文件" HorizontalAlignment="Left" Margin="757,99,0,0"
x:Name="btnReadFile" VerticalAlignment="Top" Click="btnReadFile_Click"/>
<!--Buffer方式写入和读取-->
<Button Content="写入Buffer数据" HorizontalAlignment="Left" Margin="127,173,0,0"
x:Name="btnWriteBufferFile" VerticalAlignment="Top" Click="btnWriteBufferFile_Click" />
<Button Content="读取Buffer数据" HorizontalAlignment="Left" Margin="754,173,0,0"
x:Name="btnReadBufferFile" VerticalAlignment="Top" Click="btnReadBufferFile_Click"/>
<!--Stream方式写入和读取-->
<Button Content="写入Stream数据" HorizontalAlignment="Left" Margin="127,243,0,0"
x:Name="btnWriteStreamFile" VerticalAlignment="Top" Click="btnWriteStreamFile_Click" />
<Button Content="读取Stream数据" HorizontalAlignment="Left" Margin="748,243,0,0"
x:Name="btnReadStreamFile" VerticalAlignment="Top" Click="btnReadStreamFile_Click"/>
</Grid>
cs代码:
复制代码代码如下:
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{</p>
<p> public MainPage()
{
this.InitializeComponent();
}</p>
<p> /// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}</p>
<p> //获取“库\文档”文件夹
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;</p>
<p> //创建文件以及普通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();
}</p>
<p> 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 = "提示:未找到该文件,请先创建文件";
}
}</p>
<p> 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 = "提示:未找到该文件,请先创建文件";
}
}</p>
<p> 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 + "”";
}
}</p>
<p> //将String转为Buffer
private IBuffer GetBufferFromString(String str)
{
using (InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream())
{
using (DataWriter dataWriter = new DataWriter(memoryStream))
{
dataWriter.WriteString(str);
return dataWriter.DetachBuffer();
}
}
}</p>
<p> //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 = "提示:未找到该文件,请先创建文件";
}
}</p>
<p> 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开发。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]