asked
ymd._.ksyR 0Reputation points
I am a beginner in this field. I want to download a file generated by an application deployed on HoloLens2 to my PC. I found this URL1.
I tried to execute the source code of this URL1, but I could not figure out how to use the Windows.Storage
class. Therefore, I tried to use the Windows Runtime API by referring to this URL2. The error in the function is gone, but I get the following error at using Windows.Storage
in the console of Unity where I am developing HoloLens2.
The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?)
When I type using Windows.Storage
in the source code, the predictive conversion comes up.By the way, the predictive conversion of ENABLE_WINMD_SUPPORT
in the source code of URL1 does not appear.
How can I solve this problem?
Thank you in advance.
Device used
Windows 10 Pro 21H2
Visual Studio 2019 16.11.18
Unity 2020.3.15f2
Universal Windows Platform (UWP)
Universal Windows Platform (UWP)
A Microsoft platform for building and publishing apps for any Windows device.
2,468 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
HoloLens Development
HoloLens Development
HoloLens: A family of Microsoft self-contained, holographic devices that enable engagement with digital content and interaction with holograms in the surrounding environment.Development: The process of researching, productizing, and refining new or existing technologies.
233 questions
Sign in to follow
0{count} votes
Rob Van Schooneveld 181Reputation points • Microsoft Employee
2023-01-18T03:03:07.5133333+00:00 Are you targeting a UWP application in Unity? If so, that should enable code that you have in the macro's block.
I'm trying to understand your scenario a bit more though. Is your app trying to write from the HoloLens to a PC location, or are you trying to get an app running on the PC to pull a file from the HoloLens. From a network perspective, this may be blocked (or difficult to do directly using the Storage APIs). You may want to consider saving shared content to a cloud location
ymd._.ksyR 0Reputation points
2023-01-19T05:54:48.8566667+00:00 Thank you for your advice, @Rob Van Schooneveld .
Are you targeting a UWP application in Unity?
During my research, I learned that application development for HoloLens2 can be thought of as UWP application development. If this is correct, then we are targeting UWP applications in Unity.
If so, that should enable code that you have in the macro's block.
How can I find out more about this? Or can you help me with a solution?
I'm trying to understand your scenario a bit more though. Is your app trying to write from the HoloLens to a PC location, or are you trying to get an app running on the PC to pull a file from the HoloLens.
As for that question, it's the latter.
Sorry for the lack of explanation.HoloLens runs the application I created in a standalone state. The application creates a file with the contents I want at an arbitrary location in HoloLens.After closing the HoloLens application, I want to send this created file to my PC. The way I found out was to send it to my PC via USB or via Device Portal. However, it seems that the PC's access to HoloLens is restricted, so I could not download the file from HoloLens to my PC with simple file operations. Therefore, I would like to create the file in a location where the PC can access it.
From a network perspective, this may be blocked (or difficult to do directly using the Storage APIs). You may want to consider saving shared content to a cloud location
Is it difficult to solve this problem by using URL1?
Sorry for the roundabout way. I would appreciate it if you could help me with a solution.
Sign in to comment
1 answer
Sort by: Most helpful
Most helpful Newest Oldest
answered
2023-01-19T20:18:56.9+00:00 Rob Van Schooneveld 181Reputation points • Microsoft Employee
Thanks for the follow up... You have a couple different questions here, and I'll do my best to answer them.
Regarding accessing WinRT APIs in Unity, you will need to wrap them in the pre-processor directive in order not to see script errors. When you build the Universal Windows project type from the Unity Build settings, the flag will be set and your code will be included. For more information, you can refer to the Unity documentation here:
Universal Windows Platform: WinRT API in C# scripts
In general, you should consider writing your files to one of the known file locations / libraries:
Files and folders in the Music, Pictures, and Videos libraries
The question about accessing content on the device has a bit of a different answer. There are only two primary ways to connect into the device to access it:
- Using the Device Portal
- Connecting the device via USB
When you connect via the device portal, you will have access to a bit more of the files on the device, specifically the content under AppData\Local\Packages<productname>\LocalState. So if you write to this path (similar to the Unity Player Log), you will need to utilize device portal to access it via a browser experience.
When you connect via USB (and log into the device), we expose part of the storage via MTP (Media Transfer Protocol). This has a subset of the content that is available via device portal. The easiest way to access the content here is via File Explorer, and you would just need to ensure that you put the file in a known location as discussed in the article linked above.
It would be possible if you wanted to programmatically access the content via an MTP client application. One way to do this would be via the Windows Media Device Manager SDK which is included as part of the standard Windows SDK:
Windows Media Device Manager SDK
However, this would not generally enable you to see additional file locations beyond what File Explorer would typically show you.
ymd._.ksyR 0Reputation points
2023-01-23T12:55:36.2766667+00:00 Thanks for the advice, @Rob Van Schooneveld .I will consider accessing HoloLens from the device portal.
Regarding accessing WinRT APIs in Unity, you will need to wrap them in the pre-processor directive in order not to see script errors. When you build the Universal Windows project type from the Unity Build settings, the flag will be set and your code will be included. For more information, you can refer to the Unity documentation here:
With this advice I was able to deploy the app to HoloLens. Thank you very much.However, I am unable to execute the code in URL1 as I would like. The code I created is attached below.
using System.Collections;using System.Collections.Generic;using UnityEngine;using System;using TMPro;using System.IO;using System.Text;public class picker : MonoBehaviour{ // Declare public TextMeshPro txtObj; string contents = ""; // Start is called before the first frame update void Start() { SelectFolder(); } // Selecting a folder to access void SelectFolder() { contents += "0";#if ENABLE_WINMD_SUPPORT UnityEngine.WSA.Application.InvokeOnUIThread(async () => { contents += " 1"; var folderPicker = new Windows.Storage.Pickers.FolderPicker(); contents += " 2"; folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary; contents += " 3"; folderPicker.FileTypeFilter.Add("*"); contents += " 4\n"; Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync(); contents += " 5"; if (folder != null) { contents += " 6"; Windows.Storage.AccessCache.StorageApplicationPermissions .FutureAccessList.AddOrReplace("PickerdFolderToken", folder); contents += " 7"; } }, false); SaveFile(); contents += " 13"; CheckFile(); contents += " 17";#endif txtObj.text = contents; } // File Creating and File Reacing/Writing async void SaveFile() { contents += " 8";#if ENABLE_WINMD_SUPPORT if (Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.ContainsItem("PickedFolderToken")) { contents += " 9\n"; Windows.Storage.StorageFolder storageFolder = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFolderAsync("PickedFolderToken"); contents += " 10"; Windows.Storage.StorageFile sampleFile = await storageFolder.CreateFileAsync("sample.txt", Windows.Storage.CreationCollisionOption.OpenIfExists); contents += " 11"; await Windows.Storage.FileIO.WriteTextAsync(sampleFile, "Swift as a shadow"); contents += " 12"; }#endif } // Check File Exist async void CheckFile() { contents += " 14\n";#if ENABLE_WINMD_SUPPORT if (Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.ContainsItem("PickedFolderToken")) { contents += " 15"; Windows.Storage.StorageFolder storageFolder = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFolderAsync("PickedFolderToken"); contents += " 16"; if (null != storageFolder.TryGetItemAsync("sample.txt")) contents = "\nOK"; else contents = "\nNG"; }#endif }}
When I run the app on HoloLens, the folder specification screen appears and I can specify a folder. However, there is no subsequent action. I believe the expected behavior is that
sample.txt
is generated in the specified folder. (URL1)What could be the problem?
Note that the process of adding numbers to the contents string variable in the function is to confirm that each line has been executed. At the end, the contents string variable is displayed to check which process has not been completed. Ideally all numbers would be displayed, but only 0 is displayed.
Thank you in advance.
Sign in to comment
Sign in to answer