2012-09-12 42 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) 
     { 
     } 
    }