Hololens创建、读取、删除本地文件,为解决的坑

参考资料

1.微软官方介绍 saving and finding your files https://developer.microsoft.com/en-us/windows/mixed-reality/saving_and_finding_your_files

2.自定义文件存取方法  http://longqian.me/2017/02/08/hololens-file-transfer/

 

以下是之前采用的方法,属于网上关于UWP存取文件推荐的方法。

按照2中的方法,可以将文件保存在 ApplicationData.Current.RoamingFolder文件加下,这样从网页连接hololens时就可以拷贝出来了,比较方便。

写入与读取的方法可以采用uwp提供的方式,示例如下

写入
StorageFolder folder;
        folder = ApplicationData.Current.RoamingFolder;
        StorageFile file = await folder.CreateFileAsync("position.txt", CreationCollisionOption.ReplaceExisting);
        using (StorageStreamTransaction transaction = await file.OpenTransactedWriteAsync())
        {
            using (DataWriter dataWriter = new DataWriter(transaction.Stream))
            {
                dataWriter.WriteString(positionstring);
                transaction.Stream.Size = await dataWriter.StoreAsync();
                await transaction.CommitAsync();
            }
        }

读取

StorageFolder folder;
        folder = ApplicationData.Current.RoamingFolder;
StorageFile file = await folder.TryGetItemAsync("position.txt") as StorageFile;
if (file != null)
        {
            string positionstring = await FileIO.ReadTextAsync(file);
}

读取时用的folder.TryGetItemAsync,这个方法如果打开文件失败的话会返回null,方便后续操作。

在用于hololens读取自定义的文件时,总是随机的出现闪退现象,全是在读取、写入、删除文件的地方出问题。最后用了system.io.File方法重写的,居然就再也没出闪退的问题。虽然不知道原理不过也算是解决了,但是如果自定义的文件过大的话也许会有问题。

 

posted @ 2017-05-15 20:55  不务正业的人  阅读(1884)  评论(0编辑  收藏  举报