Hololens 读写本地配置文件

本文介绍在Unity开发的Hololens UWP应用中,如何使用StorageFile进行配置文件的读写操作,遵循UWP文件操作规范,实现设备唯一标识的保存与验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hololens 读写本地配置文件

用Unity开发适用于Hololens的UWP平台应用,会发现Application.streamingAssetsPath路径是不存在的,其他几个路径也是类似,如果需要进行读写配置文件,我们应该怎么做呢?

既然开发的UWP应用,就必须要遵循UWP读写文件的规则,在UWP里多用StorageFile来读写文件
StorageFile没有直接的打开一个文件的做法,而是通过StorageFolder创建,打开文件来进行

下面的代码是一个简单的读取Hololens硬件码并保存在本地,每次启动时进行比对,不一样就退出程序的脚本

using UnityEngine;
using Windows.Storage;

public class SimpleAuthorization : MonoBehaviour
{
    private string _systemInfo;

    private void Start ()
    {
        string deviceUniqueIdentifier = SystemInfo.deviceUniqueIdentifier;
        string graphicsDeviceID = SystemInfo.graphicsDeviceID.ToString();
        _systemInfo = deviceUniqueIdentifier + " : " + graphicsDeviceID;

        WriteFileAsync();

        ReadFileAsync();
    }

    // 读取
    private async System.Threading.Tasks.Task ReadFileAsync()
    {
        Windows.Storage.StorageFolder folder;
        folder = Windows.Storage.ApplicationData.Current.RoamingFolder;
        Windows.Storage.StorageFile file = await folder.TryGetItemAsync("SystemInfo.xml") as Windows.Storage.StorageFile;
        if (file != null)
        {
            string positionstring = await Windows.Storage.FileIO.ReadTextAsync(file);
            if (_systemInfo != positionstring)
            {
                Application.Quit();
            }
        }
        else
        {
            Application.Quit();
        }
    }

    // 写入
    private async System.Threading.Tasks.Task WriteFileAsync()
    {
        Windows.Storage.StorageFolder folder;
        folder = Windows.Storage.ApplicationData.Current.RoamingFolder;
        Windows.Storage.StorageFile file = await folder.CreateFileAsync("SystemInfo_backup.xml", Windows.Storage.CreationCollisionOption.ReplaceExisting);
        using (Windows.Storage.StorageStreamTransaction transaction = await file.OpenTransactedWriteAsync())
        {
            using (Windows.Storage.Streams.DataWriter dataWriter = new Windows.Storage.Streams.DataWriter(transaction.Stream))
            {
                dataWriter.WriteString(_systemInfo);
                transaction.Stream.Size = await dataWriter.StoreAsync();
                await transaction.CommitAsync();
            }
        }
    }
}

可以进入Hololens控制台, System -> File explorer -> LocalAppdata -> 应用程序包名 -> RoamingState 中查看写入的配置文件

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值