2012-09-12 40 views
0

我是Windows手機的新手。我成功地從一個文件中書寫和閱讀字典。但是我從一個文件中讀取嵌套字典時遇到了困難。從文件中讀取和寫入嵌套字典?

  1. Main_dictionary
    1. 登錄(密鑰),字典(值)`
    2. 驗證(密鑰),字典(值)
  2. Main_dictionary

我需要寫這些值在Common字典下的一個文件中,並且還需要從相同的文件中讀取。任何幫助。

由於提前

+0

請正確格式化文件內容。 – abatishchev

回答

0

我得到的溶液...........

公共字典FILEREAD(字符串密鑰) { 字典> FileResponse =新詞典>(); Dictionary ReturnDictionary = new Dictionary(); 嘗試 { 使用(IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()){ 使用 (IsolatedStorageFileStream的FileReader =新IsolatedStorageFileStream(DisplayMessage.Storage_Directory,FileMode.Open,FileAccess.ReadWrite,isolatedStorage)) { 的DataContractSerializer datacontract =新的DataContractSerializer (typeof運算(詞典>)); FileResponse =(Dictionary>)datacontract.ReadObject(fileReader); ReturnDictionary = FileResponse [Key]; } } } 趕上(異常前) { } 回報(ReturnDictionary); }

public void FileWrite(string Key,Dictionary<string, string> FiletoStore) 
    { 
     try 
     { 
      using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       Dictionary<string, Dictionary<string, string>> StoredDictionary = new Dictionary<string, Dictionary<string, string>>(); 
       if (!isolatedStorage.FileExists(DisplayMessage.Storage_Directory)) 
       { 
        using (IsolatedStorageFileStream IsolatedfileStream = new IsolatedStorageFileStream(DisplayMessage.Storage_Directory, FileMode.OpenOrCreate, isolatedStorage)) 
        { 
         DataContractSerializer datacontract = new DataContractSerializer(typeof(Dictionary<string, Dictionary<string, string>>)); 
         StoredDictionary.Add(Key, FiletoStore); 
         datacontract.WriteObject(IsolatedfileStream, StoredDictionary); 
         IsolatedfileStream.Close(); 
        } 
       } 
       else 
       { 
        using (IsolatedStorageFileStream IsolatedfileStream = new IsolatedStorageFileStream(DisplayMessage.Storage_Directory, FileMode.Open, isolatedStorage)) 
        { 
         DataContractSerializer datacontract = new DataContractSerializer(typeof(Dictionary<string, Dictionary<string, string>>)); 
         StoredDictionary.Add(Key, FiletoStore); 
         datacontract.WriteObject(IsolatedfileStream, StoredDictionary); 
         IsolatedfileStream.Close(); 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
     } 
    }