Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

File.WriteAllBytes help

Discussion in 'AR/VR (XR) Discussion' started by ThreeTreeTrolls, Jun 20, 2018.

  1. ThreeTreeTrolls

    ThreeTreeTrolls

    Joined:
    Jul 20, 2017
    Posts:
    1
    Hey there, I'm pretty new to coding and I'm trying to figure out how to write text files to the HoloLens.

    Got one method using the Microsoft storagefile class to work, but I'm trying to get my code running using the UnityEngine.Windows class and it doesn't seem to create a new file. I'll post both scripts below starting with the one that doesn't work.

    If anyone could explain UnityEngine.Windows.File.WriteAllBytes a bit more that'd be awesome, the documentation doesn't have a fleshed out example so I'm not sure if I'm doing it right :)

    Thanks!

    P.S. Let me know if there's any other information I could provide
    I'm using unity 2018.1f1, and checking the User Files \ LocalAppData \PROGRAM\LocalState folder from my device portal.

    Code (CSharp):
    1. using System;
    2. using System.Text;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. #if UNITY_WINRT
    8. using UnityEngine.Windows;
    9. using File = UnityEngine.Windows.File;
    10. using Directory = UnityEngine.Windows.Directory;
    11.  
    12. #else
    13. using System.IO;
    14. using File = System.IO.File;
    15. using Directory = System.IO.Directory;
    16. #endif
    17.  
    18. public class WriteFileUnity : MonoBehaviour {
    19.  
    20.         const string filePath = "ApplicationData.Current.LocalFolder.Jello.txt";
    21.  
    22.         const  string fileText = "JelloWorld";
    23.         byte[] bytes = Encoding.ASCII.GetBytes(fileText);
    24.  
    25.         // Use this for initialization
    26.         void Start() {
    27.         File.WriteAllBytes(filePath, bytes);
    28.  
    29.         }
    30.  
    31.         // Update is called once per frame
    32.         void Update() {
    33.  
    34.         }
    35.  
    36.    
    37. }
    The MS File script that is working

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5.  
    6. #if WINDOWS_UWP
    7. using Windows.Storage;
    8. using System.Threading.Tasks;
    9. using Windows.Data.Xml.Dom;
    10. using System;
    11. #endif
    12.  
    13. public class WriteFile : MonoBehaviour {
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         createFile();
    18.  
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update () {
    23.        
    24.     }
    25.  
    26.     public void createFile()
    27.     {
    28.  
    29. #if WINDOWS_UWP      
    30.  
    31.         Task task = new Task(
    32.  
    33.             async () =>
    34.             {                            
    35.                 //Get local folder
    36. StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    37.  
    38. //Create file
    39. StorageFile textFileForWrite = await storageFolder.CreateFileAsync("LocalText.txt");
    40.  
    41. //Write to file
    42. await FileIO.WriteTextAsync(textFileForWrite, "Text written to file from code");
    43.  
    44.             });
    45.         task.Start();
    46.         task.Wait();
    47.  
    48. #endif
    49.  
    50.     }
    51.  
    52. }
    53.  
     
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,091