Windows 8技巧:關(guān)于windows 8的文件管理 File創(chuàng)建和String Stream Buffer方式讀寫的

在本文中我們將學(xué)習(xí)Windows 8中的文件創(chuàng)建和多種讀寫方式以及設(shè)置文檔庫訪問權(quán)限和文件類型的訪問。
當(dāng)然我們需要做以下準(zhǔn)備工作:
首先:設(shè)置程序允許訪問的文件位置為:"庫\文檔",設(shè)置方法:點(diǎn)擊”Package.appxmanifest“,然后選擇”功能“選項(xiàng)卡,在功能列表中勾選”文檔庫訪問“。如下圖:
其次:設(shè)置程序允許以上文件夾內(nèi)的文件類型,本實(shí)例中設(shè)置為txt后綴的文件:設(shè)置方法:點(diǎn)擊”Package.appxmanifest“,然后選擇”聲明“選項(xiàng)卡,在可用聲明下拉列表中選擇”文件類型關(guān)聯(lián)“,點(diǎn)擊”添加“按鈕,并且在右邊的列表中添加”支持的文件類型為.txt,并且設(shè)置名稱為txt,當(dāng)然你也可以繼續(xù)添加允許訪問dat文件等,如下圖:
準(zhǔn)備工作做好了,我們需要?jiǎng)?chuàng)建一個(gè)項(xiàng)目,然后寫入一下代碼進(jìn)行訪問文件以及文件夾,創(chuàng)建文件和讀寫文件。
Xaml代碼:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<!--顯示區(qū)-->
<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="默認(rèn)需要添加的文件內(nèi)容" VerticalAlignment="Top" Width="431" Name="tbContent"/>
<TextBlock HorizontalAlignment="Left" Margin="396,42,0,0" TextWrapping="Wrap" Text="文件內(nèi)容:"
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"/>
<!--創(chuàng)建文件以及普通string寫入讀取文本-->
<Button Content="創(chuàng)建文件" 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數(shù)據(jù)" HorizontalAlignment="Left" Margin="127,173,0,0"
x:Name="btnWriteBufferFile" VerticalAlignment="Top" Click="btnWriteBufferFile_Click" />
<Button Content="讀取Buffer數(shù)據(jù)" HorizontalAlignment="Left" Margin="754,173,0,0"
x:Name="btnReadBufferFile" VerticalAlignment="Top" Click="btnReadBufferFile_Click"/>
<!--Stream方式寫入和讀取-->
<Button Content="寫入Stream數(shù)據(jù)" HorizontalAlignment="Left" Margin="127,243,0,0"
x:Name="btnWriteStreamFile" VerticalAlignment="Top" Click="btnWriteStreamFile_Click" />
<Button Content="讀取Stream數(shù)據(jù)" 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> //創(chuàng)建文件以及普通string寫入讀取文本
private async void btnCreateFile_Click(object sender, RoutedEventArgs e)
{
StorageFile sf= await storageFolder.CreateFileAsync(this.tbFileName.Text.Trim(),
CreationCollisionOption.ReplaceExisting);
tb_show.Text = "提示:創(chuàng)建了文件--" + 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 = "提示:文件寫入成功,寫入內(nèi)容為-“" + writestr + "”";
}
catch (Exception ex)
{
tb_show.Text = "提示:未找到該文件,請先創(chuàng)建文件";
}
}</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方式讀取成功,讀取的內(nèi)容為-“" + filecontent+"”";
}
catch (Exception ex)
{
tb_show.Text = "提示:未找到該文件,請先創(chuàng)建文件";
}
}
//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 = "提示:文件寫入成功,寫入內(nèi)容為-“" + writestr + "”";
}
catch (Exception ex)
{
tb_show.Text = "提示:未找到該文件,請先創(chuàng)建文件";
}
}</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方式讀取成功,讀取的內(nèi)容為-“" + filecontent + "”";
}
}</p> <p> //將String轉(zhuǎn)為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 = "提示:文件寫入成功,寫入內(nèi)容為-“" + writestr + "”";
}
}
}
catch (Exception ex)
{
tb_show.Text = "提示:未找到該文件,請先創(chuàng)建文件";
}
}</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方式讀取成功,讀取的內(nèi)容為-“" + filecontent + "”";
}
}
}
}
}
如需源碼請點(diǎn)擊 Win8File_jb51net.rar 下載。VS2012+Windows8開發(fā)。
相關(guān)文章
Win8.1 KB5021294補(bǔ)丁月度更新匯總!(附完整更新日志)
微軟已向用戶發(fā)布了Win8.1月度更新匯總,補(bǔ)丁為KB5021294,下文為大家?guī)砹嗽敿?xì)的更新介紹,需要的朋友一起看看吧2022-12-15微軟警告:Win8.1系統(tǒng)即將停止支持,將很快彈窗提醒,建議購買 Win11/1
6月24日消息,據(jù)報(bào)道,微軟正準(zhǔn)備開展行動(dòng),通知Windows8.1用戶關(guān)于該產(chǎn)品的停止支持日期,下面隨小編一起來看看吧2022-06-24win8系統(tǒng)怎么加密文件?win8系統(tǒng)加密文件詳細(xì)操作方法
這篇文章主要介紹了win8系統(tǒng)怎么加密文件?win8系統(tǒng)加密文件詳細(xì)操作方法的相關(guān)資料,需要的朋友可以參考下本文詳細(xì)內(nèi)容介紹2022-06-13win8系統(tǒng)如何設(shè)置開機(jī)密碼?Win8系統(tǒng)設(shè)置開機(jī)密碼操作教程
這篇文章主要介紹了win8系統(tǒng)如何設(shè)置開機(jī)密碼?Win8系統(tǒng)設(shè)置開機(jī)密碼操作教程的相關(guān)資料,需要的朋友可以參考下本文詳細(xì)內(nèi)容介紹2022-06-13Win8系統(tǒng)在后臺(tái)運(yùn)行打印機(jī)程序操作教程
這篇文章主要介紹了Win8系統(tǒng)在后臺(tái)運(yùn)行打印機(jī)程序操作教程的相關(guān)資料,需要的朋友可以參考下本文詳細(xì)內(nèi)容介紹2022-06-13Win8系統(tǒng)安全證書過期怎么辦?Win8系統(tǒng)安全證書過期的解決方法
這篇文章主要介紹了Win8系統(tǒng)安全證書過期怎么辦?Win8系統(tǒng)安全證書過期的解決方法的相關(guān)資料,需要的朋友可以參考下本文詳細(xì)內(nèi)容介紹2022-06-13win8系統(tǒng)還原怎么操作呢?win8電腦系統(tǒng)還原詳細(xì)步驟
如何解決Win10瀏覽器打不開網(wǎng)頁但能上網(wǎng)?最近很多用戶向小編反應(yīng)升級(jí)了win10系統(tǒng)之后在下載軟件或者打開第三方網(wǎng)頁的時(shí)候會(huì)出現(xiàn)病毒誤報(bào),該怎么辦呢?跟隨小編來看看吧2020-12-31- Win8臨時(shí)文件夾如何修改路徑?Win8臨時(shí)文件夾文件過多會(huì)影響速度,我們可以通過修改文件路徑很好的解決這一問題,下面讓我們來看看如何修改2020-12-22
Win8系統(tǒng)如何設(shè)置鼠標(biāo)左右鍵互換?
Win8系統(tǒng)如何設(shè)置鼠標(biāo)左右鍵互換?最近很多小伙伴向小編咨詢關(guān)于Win8系統(tǒng)設(shè)置鼠標(biāo)左右鍵互換的問題,下面就跟隨小編一起學(xué)習(xí)吧2020-12-11Win8系統(tǒng)怎么關(guān)閉系統(tǒng)還原?
Win8系統(tǒng)怎么關(guān)閉系統(tǒng)還原?最近很多win8系統(tǒng)的用戶向小編咨詢想要關(guān)閉系統(tǒng)還原,不知道如何操作,不妨我們一起來看看下文的教程吧2020-12-09