Win8 Metro中檔案讀寫刪除與複製操作

2021-09-07 21:59:44 字數 3807 閱讀 1179

win8metro中,我們不能在向以前那樣呼叫win32的api函式來進行檔案操作,因此,下面就來介紹一下win8 metro中檔案的讀寫操作。

包括storagefolder,storagefile,fileio等類庫

。2 win8檔案的讀寫操作都是

非同步方式進行的,因此要使用async

3 建立檔案: 

storagefile storagefile=await windows.storage.knownfolders.documentslibrary.createfileasync("1.txt",windows.storage.creationcollisionoption.replaceexisting);
這裡我們建立了乙個1.txt的文件,如果已經存在這個文件,那麼新建的文件將替換,覆蓋掉舊文件。

由於文件讀寫是非同步方式操作,因此,我們要將它放到async修飾的函式裡才可以使用,具體如下:

private async void selectimageone(byt outarary)

在上述的**中,引數是我們要寫入到檔案「1.txt」裡的內容,這裡是乙個byte陣列。

4 寫入檔案:

如3中的**所示await fileio.writebytesasync(storagefile, outarary);

寫入檔案的方法是fileio中的write方法,這裡一共有以下四種方法:

writebufferasync(windows.storage.istoragefile file, ibuffer buffer);

writebytesasync(windows.storage.istoragefile file, byte buffer);

writelinesasync(windows.storage.istoragefile file, ienumerablelines);

writelinesasync(windows.storage.istoragefile file, ienumerablelines,

unicodeencoding encoding);

writetextasync(windows.storage.istoragefile file, string contents);

writetextasync(windows.storage.istoragefile file, string contents,

unicodeencoding encoding);

這裡我們列舉的是寫入byte的方法。

5 開啟檔案:

storagefile storagefile=await windows.storage.knownfolders.documentslibrary.createfileasync("1.txt",windows.storage.creationcollisionoption. openifexists);
這裡我們開啟了乙個名字為」1.txt」的文字檔案。

6 讀取檔案:

在fileio中有三種檔案讀取方法,分別讀取不同的檔案:

await fileio.readtextasync(windows.storage.istoragefile file);

await fileio.readtextasync(windows.storage.istoragefile file, unicodeencoding encoding);//返回指定的文字編碼格式

await fileio. readbufferasync (windows.storage.istoragefile file);

await fileio. readlinesasync (windows.storage.istoragefile file);

await fileio. readlinesasync (windows.storage.istoragefile file, unicodeencoding encoding);

這裡我們以文字為例:

string fileicontent = await fileio. readtextasync (storagefile);

這樣我們就返回了乙個string文字。

我們也可以通過流來讀取檔案:

ibuffer buffer = await fileio.readbufferasync(storagefile);

using (datareader datareader = datareader.frombuffer(buffer))

7 ibuffer, byte, stream之間的相互轉換:

storagefile storagefile=await

windows.storage.knownfolders.documentslibrary.createfileasync("1.txt",windows.storage.creationcollisionoption. openifexists);

ibuffer buffer = await fileio.readbufferasync(storagefile);

byte bytes=windowsruntimebufferextensions.toarray(buffer,0,(int)buffer.length);

stream stream = windowsruntimebufferextensions.asstream(buffer);

另外乙個例項:

1.首先建立乙個資料夾,在資料夾裡建立檔案

private async void createbutton_click(object sender, routedeventargs e)

2.在建立好的檔案裡,寫入我們的資料,這裡介紹三種寫入檔案的方式

private async void writebutton_click(object sender, routedeventargs e)

}

3.讀取剛才寫入檔案裡的資料,這裡也介紹三種讀取檔案的方式

private async void readbutton_click(object sender, routedeventargs e)

showtextbox.text = content;

} private ibuffer convert(string text)  //將string轉換成ibuffer型別的}}

private string convert(ibuffer buffer)    //將ibuffer轉換成string型別的

return text;

}private async taskconvert()}}

return text;

}4.讀取檔案的屬性

private async void readpropertybutton_click(object sender, routedeventargs e)

property = extra["system.fileowner"];

if(property!=null)

displyproperty.text = builder.tostring();}}

}5.複製刪除檔案  

private async void okbutton_click(object sender, routedeventargs e)

if (copyobutton.ischecked.value) //判斷進行複製還是刪除

else

}catch

}

Win8 Metro風格應用開發學習路徑

不定期更新!最新 2011 11 18 已有知識點 vc6 mfc sdk 最開始接觸win32 sdk開發,近幾年用於開發產品和共享軟體 mfc,gdi plus 近兩年開發的 軟體 倒計時軟體中普遍採用。介面輸出效果很好。html js asp php 多個 後台基於asp mdb,php my...

Win8檔案讀寫

和.net一樣,win8應用商店程式也提供了很多操作檔案的類,今天學習了幾個常用得操作。1.讀取和寫入本地檔案庫檔案。在操作之前需要在應用程式清單中新增相應的功能。如下圖 接著還沒有全部完成,切換到宣告選項卡,操作如下 在這張圖的下面還有乙個,在支援檔案型別中有個支援檔案的型別,可以填,需要操作檔案...

Metro中資料夾和檔案的建立

使用之前,需要引用命名空間 using windows.storage 該類只要是把檔案及資料夾儲存到當前登入使用者的目錄下 roamingstate 紅色部分的資料夾名稱,不同的應用程式不一樣,可以不用關心。storagefolder tempfolder await folder.createf...