UWP应用程序从系统上的随机位置访问文件 [英] UWP apps accessing files from random location on system

查看:101
本文介绍了UWP应用程序从系统上的随机位置访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UWP中有文件和权限限制,因此我们只能直接从几个文件夹访问文件,也可以使用filepicker从系统上的任何位置进行访问. 我如何使用从文件选择器中选取的文件,并在应用程序运行时再次使用它们?试图通过路径再次使用它们,但它给出了权限错误.我知道"futureacceslist",但是它的限制是1000,如果我没有记错的话,它还会使应用变慢吗? . 有一个更好的方法吗 ?还是我们可以通过某种方式将存储文件链接存储在本地sqlite数据库中?

in UWP there are files and permissions restrictions, so we can only acces files directly from few folders or we can use filepicker to access from anywhere on system. how can I use the files picked from filepicker and use them anytime again when the app runs ? tried to use them again by path but it gives permission error. I know about the "futureacceslist" but its limit is 1000 and also it will make the app slow if I am not wrong? . Is there a better way to do this ? or can we store storage files link somehow in local sqlite database?

推荐答案

考虑此方法..

    public async static Task<byte[]> ToByteArray(this StorageFile file)
    {
        byte[] fileBytes = null;
        using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync())
        {
            fileBytes = new byte[stream.Size];

            using (DataReader reader = new DataReader(stream))
            {
                await reader.LoadAsync((uint)stream.Size);
                reader.ReadBytes(fileBytes);
            }
        }

        return fileBytes;
    }

这堂课..

    public class AppFile
    {
        public string FileName { get; set; }
        public byte[] ByteArray { get; set; }
    }

这个变量

    List<AppFile> _appFiles = new List<AppFile>();

就..

    var fileOpenPicker = new FileOpenPicker();
    IReadOnlyList<StorageFile> files = await fileOpenPicker.PickMultipleFilesAsync();

    foreach (var file in files)
    {
        var byteArray = await file.ToByteArray();
        _appFiles.Add(new AppFile { FileName = file.DisplayName, ByteArray = byteArray });
    }

更新

using Newtonsoft.Json;
using System.Linq;
using Windows.Security.Credentials;
using Windows.Storage;

namespace Your.Namespace
{
    public class StateService
    {
        public void SaveState<T>(string key, T value)
        {
            var localSettings = ApplicationData.Current.LocalSettings;
            localSettings.Values[key] = JsonConvert.SerializeObject(value);
        }

        public T LoadState<T>(string key)
        {
            var localSettings = ApplicationData.Current.LocalSettings;
            if (localSettings.Values.ContainsKey(key))
                return JsonConvert.DeserializeObject<T>(((string)    localSettings.Values[key]));
            return default(T);
        }

        public void RemoveState(string key)
        {
            var localSettings = ApplicationData.Current.LocalSettings;
            if (localSettings.Values.ContainsKey(key))
                localSettings.Values.Remove((key));
        }

        public void Clear()
        {
            ApplicationData.Current.LocalSettings.Values.Clear();
        }
    }
}

这篇关于UWP应用程序从系统上的随机位置访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆