【WIN10】程序內文件讀取與保存
DEMO下載:http://yunpan.cn/cFHIZNmAy4ZtH ?訪問密碼 cf79
?
1.讀取與保存文件
Assets一般被認(rèn)為是保存用戶文件數(shù)據(jù)的地方。同時(shí),微軟還支持用戶自己創(chuàng)建文件夾。如下圖:
可以創(chuàng)建自己的文件夾data,當(dāng)然命名可以任意命名,然後往裡面添加文件。我們?cè)賮砜纯磘est.txt的屬性,如下圖:
如果開發(fā)過WP8程序的人,可能對(duì)這個(gè)會(huì)比較印象深刻。WP8開發(fā)中,“生成操作”需要手動(dòng)設(shè)置為“內(nèi)容”,在WIN10中,已經(jīng)默認(rèn)為“內(nèi)容”了,這才是比較正常的處理,我們加一個(gè)文件,不就是為了放到程序內(nèi)部嗎?為毛還要手動(dòng)去設(shè)置?WIN10開發(fā)顯示就比較合理了。
?
我寫了一個(gè) Common 的類,用於打開文件,以讀及寫:
public class Common{public static async Task<Stream> OpenFileForRead(string folder, string fileName, bool installFolder = true){try{StorageFolder storageFolder = null;if (installFolder) storageFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(folder);else storageFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync(folder);StorageFile storageFile = await storageFolder.GetFileAsync(fileName);return await storageFile.OpenStreamForReadAsync();}catch (Exception ex){return null;}}public static async Task<Stream> OpenFileForWrite (string folder, string file){try{StorageFolder rootFolder = ApplicationData.Current.LocalFolder;StorageFolder dstFolder = await rootFolder.CreateFolderAsync(folder, CreationCollisionOption.OpenIfExists);if (dstFolder != null){StorageFile storageFile = await dstFolder.CreateFileAsync(file, CreationCollisionOption.ReplaceExisting);return await storageFile.OpenStreamForWriteAsync();}}catch (Exception){}return null;}}?
你會(huì)發(fā)現(xiàn),它們都是“異步”函數(shù)。所以要程序讀取一些信息並顯示時(shí),可能需要一個(gè)“委託”,當(dāng)讀完時(shí),通知界面去顯示相關(guān)信息,或是其它處理。
?
*****************************************************************************************************************************************************************
注意上面的代碼,寫文件,只能在該文件夾下寫:ApplicationData.Current.LocalFolder
?安裝目錄?Windows.ApplicationModel.Package.Current.InstalledLocation 是不允許寫的。
*****************************************************************************************************************************************************************
?
我的DEMO中,不僅加入了創(chuàng)建文件的代碼,還加入了創(chuàng)建文件夾的代碼。
?
2.使用Roaming進(jìn)行保存與讀取數(shù)據(jù)
這個(gè)東東,相當(dāng)於程序的系統(tǒng)中的配置。使用它進(jìn)行保存數(shù)據(jù)的好處是,當(dāng)程序更新時(shí),你做的保存不會(huì)丟失。
如果是使用第一種方法進(jìn)行保存程序數(shù)據(jù)的話,當(dāng)軟件更新時(shí),有可能會(huì)將用戶保存的數(shù)據(jù)覆蓋。那樣問題就比較嚴(yán)重了,用戶今天還好好的,晚上WIFI自動(dòng)更新後,一覺起來,神馬都沒有了,突然間整個(gè)人都不好了。
?
微軟提供的Roaming是相當(dāng)強(qiáng)悍的,可以像Json一樣使用,具體內(nèi)部是不是使用Json實(shí)現(xiàn),我沒有深入研究過,不過我猜應(yīng)該是。
可以創(chuàng)建Container,然後在Container中保存一堆[key,value]組合,Container中又可以創(chuàng)建Container。和Json一個(gè)樣。這種分層保存數(shù)據(jù)十分適合軟件吶,如果只有一層[key,value],那要加一堆前輟進(jìn)行說明這個(gè)key是誰誰誰就蛋疼了。
?
讀的例子:
private void readRoamingBtn_Click(object sender, RoutedEventArgs e){var appSetting = ApplicationData.Current.RoamingSettings;var trueContainer = appSetting;if (IsCreateContainer()){ApplicationDataContainer adc = null;if (!appSetting.Containers.TryGetValue("test", out adc)){MessageDialog dlg = new MessageDialog("no container");dlg.ShowAsync();return;}trueContainer = adc;}object testValue = null;if (trueContainer.Values.TryGetValue("key", out testValue)){roadmingReadTxtBox.Text = testValue.ToString();}else{MessageDialog dlg = new MessageDialog("no key");dlg.ShowAsync();}}?
寫的例子:
private void roamingWriteBtn_Click(object sender, RoutedEventArgs e){var appSetting = ApplicationData.Current.RoamingSettings;var trueContainer = appSetting;if (IsCreateContainer()){trueContainer = MakeSureArticleCfgContainerExist(appSetting, "test");}trueContainer.Values["key"] = roamingWriteTxtBox.Text;}?
有一個(gè)?MakeSureArticleCfgContainerExist 函數(shù),用於確定寫入的文件夾一定存在,如果不存在就創(chuàng)建它:
private ApplicationDataContainer MakeSureArticleCfgContainerExist(ApplicationDataContainer appSetting, string name){ApplicationDataContainer container = null;if (!appSetting.Containers.TryGetValue(name, out container)){container = appSetting.CreateContainer(name, ApplicationDataCreateDisposition.Always);}return container;}?
3.使用Resource
資源文件又被微軟給閹割了,它只能使用 String.
使用方法:
?1)添加Resource
?菜單:右鍵項(xiàng)目->添加->新建項(xiàng)->資源Resource
?
?2)加載Resource
ResourceLoader rl = new ResourceLoader();txtResource.Text = rl.GetString("String1");?
[END]
轉(zhuǎn)載于:https://www.cnblogs.com/lin277541/p/4888256.html
總結(jié)
以上是生活随笔為你收集整理的【WIN10】程序內文件讀取與保存的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android之音乐播放和音效播放
- 下一篇: POJ 2010 Moo Univer