Hololens2 (UWP) create folders and files

  • In the development of Hololens2, it is often necessary to create folders and files. The method is different from that of C# ordinary Windows PC. You can create files and folders in the following ways.

  • Create a file in the "Music" or "PictureLibrary" folder, the file is a byte stream format:

  private async void CreatFileToPicLibrary(string Filename, byte[] Bytedata)
        {
    
    
            StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null /* current user */, KnownFolderId.PicturesLibrary);
            try
            {
    
    
                var file = await storageFolder.CreateFileAsync(Filename, CreationCollisionOption.ReplaceExisting);
                FileIO.WriteBytesAsync(file, Bytedata);

            }
            catch (Exception ex)
            {
    
    

            }
        }

If you want to create a new folder inside, you can change it to the following code:

           private async void CreatFileToPicLibrary(string Filename, byte[] Bytedata)
        {
    
    
            StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null /* current user */, KnownFolderId.PicturesLibrary);
            StorageFolder storageFolder2 = await storageFolder.CreateFolderAsync("intric");
            try
            {
    
    
                var file = await storageFolder2.CreateFileAsync(Filename, CreationCollisionOption.ReplaceExisting);
                FileIO.WriteBytesAsync(file, Bytedata);

            }
            catch (Exception ex)
            {
    
    

            }
        }
  • New files need to add permissions:
    insert image description here

- Or directly open Package.appxmanifestthe file and add the following code:
insert image description here

Guess you like

Origin blog.csdn.net/ZhangJingHuaJYO/article/details/128062373
Recommended