0

IsolatedStorageFile.FileExists(string path) works but StreamReader(string samePath) doesn't? I have validated both paths are equal. I have no idea why the StreamReader explodes

     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); 

An exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.ni.dll but was not handled in user code

Message:Could not find a part of the path 'C:\projects\Title_939931883.txt'.

DotNetRussell
  • 9,716
  • 10
  • 56
  • 111

3 Answers3

1

Give this one a try. Reading and writing files in IsolatedStorage has a different path and should be used like that. You should consider reading 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))
                    {
                    }
                }
Raz Harush
  • 739
  • 1
  • 6
  • 21
1

Path is not same in both cases. In first case you are getting User store for application and then search for file in it. But in later case you are simply searching in base directory.

StreamReader constructor expects absolute path of file.

You need to create IsolatedStorageFileStream and pass it on to StreamReader -

using (IsolatedStorageFileStream fileStream =  new IsolatedStorageFileStream
                                ("/projects/" + filename, FileMode.Open, file))
{
    using (StreamReader reader = new StreamReader(fileStream ))
    {
    }
}
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
0

This was the solution I came up with

        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();
            }
        }

I don't know why I was getting the illegal operation on boot of app but I know why it was happening later on. I guess when you try and access the same file to quickly it causes errors. So I added in a fileshare and also I made sure to dispose of other accesses before this ran.

DotNetRussell
  • 9,716
  • 10
  • 56
  • 111