2013-07-20 71 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(); 
      } 
     } 

的解决方案,我不知道为什么,我居然也得到了非法操作在应用程序启动时,但我知道为什么它发生在稍后。我想当你尝试和快速访问相同的文件会导致错误。所以我加入了一个文件共享中,并且我确保在运行之前处理其他访问。