1

I cannot access the File object in System.IO as mentioned here. A code as simple as this throws an error that FileStream or File don't exist in the current context.

FileStream fs = File.Open(filePath, FileMode.Open);
        

I'm trying to write a Windows Store app in C# in Visual Studio 2013. I've been stuck at this for hours and have no idea why it isn't working. Any help will be appreciated.

Community
  • 1
  • 1
Shashwat Black
  • 992
  • 5
  • 13
  • 24
  • 3
    If I understand correctly, the windows store security model generally restricts arbitrary file access. You can read up on some of it [here](http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html), [here](http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh758325.aspx), and [here](http://msdn.microsoft.com/en-ca/library/windows/apps/hh465199.aspx). Are you attempting to access a file in a restricted path? – Chris Sinclair Jun 28 '14 at 12:37
  • 2
    Look up Isolated Storage. It's all you will get. – H H Jun 28 '14 at 12:42
  • are you sure the path is correct and the file is there? – Neel Jun 28 '14 at 12:45

1 Answers1

0

It can be an Isolated Storage issue

The following code example obtains an isolated store and checks whether a file named TestStore.txt exists in the store. If it doesn't exist, it creates the file and writes "Hello Isolated Storage" to the file. If TestStore.txt already exists, the example code reads from the file.

using System;
using System.IO;
using System.IO.IsolatedStorage;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

            if (isoStore.FileExists("TestStore.txt"))
            {
                Console.WriteLine("The file already exists!");
                using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.Open, isoStore))
                {
                    using (StreamReader reader = new StreamReader(isoStream))
                    {
                        Console.WriteLine("Reading contents:");
                        Console.WriteLine(reader.ReadToEnd());
                    }
                }
            }
            else
            {
                using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.CreateNew, isoStore))
                {
                    using (StreamWriter writer = new StreamWriter(isoStream))
                    {
                        writer.WriteLine("Hello Isolated Storage");
                        Console.WriteLine("You have written to the file.");
                    }
                }
            }   
        }
    }
}

More about Isolated Storage

MRebai
  • 5,344
  • 3
  • 33
  • 52