2013-07-20 107 views
0

IsolatedStorageFile.FileExists(string path)作品,但StreamReader(string samePath)不? 我已驗證兩個路徑是相等的。我不知道爲什麼StreamReader的爆炸IsolatedStorageFile.FileExists(字符串路徑)工作,但StreamReader(字符串samePath)不?

 List<ProjectObj> ret = new List<ProjectObj>(); 
    IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication(); 

    if (!file.DirectoryExists("/projects/")) //trying to validate the dir exists 
     return ret; 

    string[] fileNames = file.GetFileNames("/projects/"); 

    foreach (string filename in fileNames) 
    { 
     if (!file.FileExists("/projects/" + filename)) //validate just one more time.. 
      continue; 

     ProjectObj tempProj = new ProjectObj(); 

     //Even with the validation it still breaks right here with the bellow error 
     StreamReader reader = new StreamReader("/projects/"+filename); 

型「System.IO.DirectoryNotFoundException」的異常出現在mscorlib.ni.dll 但在用戶代碼中沒有處理

消息:可能找不到路徑 'C:\ projects \ Title_939931883.txt'的一部分。

回答

1

給這個試試看。在IsolatedStorage中讀寫文件有不同的路徑,應該這樣使用。你應該考慮閱讀How to: Read and Write to Files in Isolated Storage

 public static List<ProjectObj> getProjectsList() 
     { 
      List<ProjectObj> ret = new List<ProjectObj>(); 
      IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication(); 

      if (!file.DirectoryExists("/projects/")) //trying to validate the dir exists 
       return ret; 

      string[] fileNames = file.GetFileNames("/projects/"); 

      foreach (string filename in fileNames) 
      { 
       if (!file.FileExists("/projects/" + filename)) //validate just one more time... 
        continue; 

       ProjectObj tempProj = new ProjectObj(); 

       using (var isoStream = new IsolatedStorageFileStream("/projects/" + filename, FileMode.Open, FileAccess.Read, FileShare.Read, file)) 
       { 
        using (StreamReader reader = new StreamReader(isoStream)) 
        { 
        } 
       } 
+0

IsolatedStorageFileStream不允許操作。 –

+0

我已經更新了答案並使用http://stackoverflow.com/questions/8415979/operation-not-permitted-on-isolatedstoragefilestream-error –

+0

修復了第一條使用線上的錯誤 –

1

這兩種情況下的路徑都不相同。在first case你得到User store for application,然後在其中搜索文件。但在later case中,您只需在base directory中搜索。

StreamReader構造函數預計文件的absolute path

您需要創建IsolatedStorageFileStream,並把它傳遞給StreamReader -

using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream 
           ("/projects/" + filename, FileMode.Open, file)) 
{ 
    using (StreamReader reader = new StreamReader(fileStream)) 
    { 
    } 
} 
+0

不允許的操作IsolatedStorageFileStream。 –

+0

您可以對IsolatedStorageFileStream執行操作。從MSDN請參閱此鏈接 - http://msdn.microsoft.com/en-us/library/xf96a1wz.aspx –

0

這是我想出了

 List<ProjectObj> ret = new List<ProjectObj>(); 
     IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication(); 

     if (!file.DirectoryExists("/projects/")) 
      return ret; 
     foreach (String filename in file.GetFileNames("/projects/")) 
     { 
      IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); 
      IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("/projects/"+filename, FileMode.Open, FileAccess.Read); 
      using (StreamReader reader = new StreamReader(fileStream)) 
      { 
       String fileInfo = reader.ReadToEnd(); 
      } 
     } 

的解決方案,我不知道爲什麼,我居然也得到了非法操作在應用程序啓動時,但我知道爲什麼它發生在稍後。我想當你嘗試和快速訪問相同的文件會導致錯誤。所以我加入了一個文件共享中,並且我確保在運行之前處理其他訪問。