2013-10-03 49 views
2

我想弄清楚如何讀寫存儲在應用程序隔離存儲中的參數。在獨立存儲,便攜式類庫中緩存變量的方式

現在即時建立一個Windows Phone應用程序,但因爲我想要一個win8應用程序,我想我可以在便攜式類庫項目中做到這一點,並發現這個真棒PCLStorage

我的緩存類看起來ATM像這樣用於存儲PARAMS:

public async static Task<string> GetParam(string name) 
    { 
     IFolder rootfolder = FileSystem.Current.LocalStorage; 
     IFolder folder = await rootfolder.GetFolderAsync("isostore"); 
     IFile file = await folder.GetFileAsync(name); 

     return await file.ReadAllTextAsync(); 
    } 

    public async static void SaveParam(string name, string param) 
    { 
     IFolder rootfolder = FileSystem.Current.LocalStorage; 
     IFolder folder = await rootfolder.CreateFolderAsync("isostore", CreationCollisionOption.OpenIfExists); 
     IFile file = await folder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting); 

     await file.WriteAllTextAsync(param); 
    } 

寫入部件是好,它覆蓋,如果有任何。它的閱讀部分就是問題所在。 IFile和IFolder沒有任何.Exists函數(???),如果我在保存之前調用Get,它會返回什麼?

回答

1

我認爲你的情況在GetParam方法你應該使用CreationCollisionOption.OpenIfExists參數調用CreateFolderAsync和CreateFileAsync。那麼你不需要擔心事先單獨創建它們,或者捕獲它們存在/不存在的異常。