locked
readalllines windows 8 app RRS feed

  • Question

  • For windows desktop i can use the code below to read lines of text file to a array but this does not work for the windows store what do i need to do for the window store

    System.IO.File.ReadAllLines(@"File Path/file.txt") 

    • Moved by CoolDadTx Friday, July 10, 2015 5:27 PM WinStore related
    Friday, July 10, 2015 4:35 PM

Answers

  • File IO doesn't work the same in WinRT as it does in normal apps because: a) apps run in a sandbox and b) most APIs are async.  Refer to this sample (and others) on MSDN on how to do file IO in a WinRT app: https://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Friday, July 10, 2015 5:26 PM
  • You need to get a reference to a StorageFile object and then you can use the ReadLinesAsync method:

    private async void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                var folder = ApplicationData.Current.LocalFolder;
                var file = await folder.GetFileAsync("filename.txt");
                IList<string> lines = await FileIO.ReadLinesAsync(file);
      //...
            }

    You could use a FileOpenPicker to let the user select the file to be read: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh771180.aspx

    But since a Windows Store App runs in a sandboxed environment you cannot just hard code the path to the file to be read anywhere on the hard drive like you can do in a traditional .NET desktop application. This is not permitted.

    Please refer to Jerry Nixon's blog for more information about how to read and write files in WinRT: http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Saturday, July 11, 2015 2:02 PM

All replies

  • File IO doesn't work the same in WinRT as it does in normal apps because: a) apps run in a sandbox and b) most APIs are async.  Refer to this sample (and others) on MSDN on how to do file IO in a WinRT app: https://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Friday, July 10, 2015 5:26 PM
  • You need to get a reference to a StorageFile object and then you can use the ReadLinesAsync method:

    private async void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                var folder = ApplicationData.Current.LocalFolder;
                var file = await folder.GetFileAsync("filename.txt");
                IList<string> lines = await FileIO.ReadLinesAsync(file);
      //...
            }

    You could use a FileOpenPicker to let the user select the file to be read: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh771180.aspx

    But since a Windows Store App runs in a sandboxed environment you cannot just hard code the path to the file to be read anywhere on the hard drive like you can do in a traditional .NET desktop application. This is not permitted.

    Please refer to Jerry Nixon's blog for more information about how to read and write files in WinRT: http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Saturday, July 11, 2015 2:02 PM