Search Unity

Hololens Load File to Stream

Discussion in 'Windows' started by DrunkReaperMatt, Jul 26, 2017.

  1. DrunkReaperMatt

    DrunkReaperMatt

    Joined:
    Feb 11, 2013
    Posts:
    38
    I'm having an issue trying to read a basic text file and load the data to my app. I'm using IL2CPP to build the app, and pre-processors to surround the async parts.

    I'm using this as my read.

    Code (CSharp):
    1. public void ReadString() {
    2.   string s;
    3. #if !UNITY_EDITOR && UNITY_METRO
    4.   try {
    5.     using (Stream stream = OpenFileForRead(ApplicationData.Current.RoamingFolder.Path, "filename.txt")) {
    6.       byte[] data = new byte[stream.Length];
    7.       stream.Read(data, 0, data.Length);
    8.       s = Encoding.ASCII.GetString(data);
    9.     }
    10.   }
    11.   catch (Exception e) {
    12.     Debug.Log(e);
    13.   }
    14. #endif
    15.   return s;
    16. }
    17.  
    18. private static Stream OpenFileForRead(string folderName, string fileName) {
    19.   Stream stream = null;
    20. #if !UNITY_EDITOR && UNITY_METRO
    21.   Task task = new Task(
    22.     async () => {
    23.       StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folderName);
    24.       StorageFile file = await folder.GetFileAsync(fileName);
    25.       stream = await file.OpenStreamForReadAsync();
    26.     });
    27.   task.Start();
    28.   task.Wait();
    29. #endif
    30.   return stream;
    31. }
    I can't seem to build this with the OpenStreamForReadAsync().
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,678
    Why not just use File.ReadAllText?
     
  3. DrunkReaperMatt

    DrunkReaperMatt

    Joined:
    Feb 11, 2013
    Posts:
    38
    does it work on the hololens?
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,678
    It sure does if you use IL2CPP.