在没有文件选择器的情况下读写硬盘上的文件夹? [英] Read and Write to and from folder on Hard Disk without File Pickers?

查看:29
本文介绍了在没有文件选择器的情况下读写硬盘上的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去 2 年中,我发布了多个 Windows 应用商店应用.虽然它们取得了一定的成功,但它们都是生产力应用程序.一位微软员工已经(向我)确认他们不允许对 DocumentsLibrary 的通用访问".这意味着(在我们所说的上下文中),即使我持有 Windows 应用商店的公司帐户,我/我们仍然不能自动访问包含在文档库中的文件,我们也不能允许将数据/文件写入文档库 - 除非我们也使用 SkyDrive - 但如果我们使用 SkyDrive,我们还必须使用桌面上的用户 SkyDrive 文件夹.这真的搞砸了应该有多少应用程序可以工作.

I've released several Windows Store apps over the last 2 years. And while they're moderately successful, they're all Productivity apps. And a Microsoft employee has confirmed (to me) that they do not allow "generic access to the DocumentsLibrary". What this means (in the context that we were speaking in), is that even if I held a Company account with the Windows Store, I/We are still not allowed to gain automatic access to files contained within the Documents Library and we are not allowed to Write data/files to the Documents Library - unless we also use SkyDrive - but if we use SkyDrive, we must also use the users SkyDrive Folder on the Desktop. And this really screws with how many apps should work.

由于对我们开发者施加了如此愚蠢的限制,我发现发布许多我有想法的应用变得越来越困难.

I am finding it increasingly more difficult to release many apps that I have ideas for, due to such silly restrictions placed on us Developers.

根据调查和研究,我们 92% 的客户不喜欢喜欢将他们的数据存储在云中,这也不是我们希望应用程序运行的一部分.无论有没有云存储,我们的应用都必须以某种方式运行.

Based on surveys and research, 92% of our customers do not like storing their data in the Cloud, and nor is it a part of how we want our apps to function. Our apps must function a certain way with and without Cloud storage.

现在,让我们看一下正常、安全的 Windows 桌面生产力应用程序的一个非常常见的场景.为了保持场景简单,我们将使用一个类似记事本的程序.唯一的区别是,它会在启动时自动将所有笔记加载到程序中.并自动确定是否需要保存更改,并在需要时进行保存.

Now, let's take a look at an all too common scenario of a normal, safe, Windows Desktop Productivity app. To keep the scenario simple, we'll be using a Notepad-like program. The only difference being, that it automatically loads all notes into the program on startup. And automatically determines if changes need to be saved, and saves them, if required.

在 Windows 8 和 8.1 中.这些应用程序已经死了.如果没有文件选择器,我们将无法访问文档库.用户要做什么?使用 Open File Picker 单独选择"/选择大约 40 个文件?以便他们可以在应用程序中随时使用它们,并在他们想要修改/查看它时单击它?真可怜.

In Windows 8 and 8.1. These apps are dead. We cannot access the Documents Library without a File Picker. What is the user going to do? Individually "pick"/select about 40 files using the Open File Picker? So that they can have them at hand in the app and just click it whenever they feel like modifying/viewing it? That's pathetic.

所以我开始使用他们过去在文档中告诉我们的 AppData 文件夹.

So I began using the AppData folder which they've told us in Documentation in the past is what that's there for.

但如果用户有意或无意地卸载应用程序 - 他们的数据就会丢失.走了.不可恢复.

But if the user Uninstalls the app, intentionally or unintentionally - their data, is lost. Gone. Unrecoverable.

(请不要认为这是咆哮.它不是.我只是详细解释问题,以防有些人不太了解我对此类访问的需求 - 或类似/相同的功能).

(Please don't think this is a rant. It's not. I am merely explaining the problem in detail in case some don't quite understand my need for such access - or similar/same functionality).

所以,我的问题是:

由于我们不能将文档库用于通用用途",哪里是安全位置来存储由其硬盘驱动器上的用户打开、创建和修改的文件?一个仍将通过应用程序认证的位置.即使应用程序被卸载,该位置仍将保留.一个我们可以毫无疑问地读取和/或写入/从的位置 - 无需使用愚蠢的文件选择器.

Since we cannot use the Documents Library for "generic use", where is a safe location to store files opened by, created and modified by, the user that is on their hard drive? A location that will still make it through App Certification. A location that will remain, even if the app gets uninstalled. A location where we have no-questions-asked access to read and/or write to/from - without the use of silly File Pickers.

推荐答案

多亏了 Nate 的建议,我才能够编写一些代码来使其工作.基本上,我请求选择一个文件夹的权限,一旦用户选择了他们想要使用的文件夹,我就会枚举其中的文件,并将它们添加到 ListView(测试它是否适用于我的情况的基本示例).

Thanks to Nate's advice, I was able to write up a bit of code to get this working. Basically, I request permission to choose a folder and once the user has chosen their folder they want to work with, I then enumerate the files within, and add them to a ListView (a basic sample to test if it will work in my situation).

XAML:

<Page
    x:Class="test.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:test"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{Binding Note}" x:Name="list">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding name}" />
                </DataTemplate>
            </ListView.ItemTemplate>

        </ListView>
    </Grid>
</Page>

C#:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace test
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        ObservableCollection<Note> noteslist = new ObservableCollection<Note>();

        public class Note
        {
            public StorageFile file { get; set; }
            public string name { get; set; }
        }
        public MainPage()
        {
            this.InitializeComponent();
        }


        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            var opener = new Windows.Storage.Pickers.FolderPicker();
            opener.FileTypeFilter.Add(".txt");

            StorageFolder folder = await opener.PickSingleFolderAsync();

            if(folder != null)
            {
                StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);

                // Got it.
                IReadOnlyList<StorageFile> files = await folder.GetFilesAsync();

                if(files != null)
                {
                    foreach(StorageFile f in files)
                    {
                        noteslist.Add(new Note()
                            {
                                name = f.DisplayName,
                                file = f
                            });

                    }

                    list.ItemsSource = noteslist;
                }
            }
        }
    }
}

由于这只是一个简单的测试,基本上零错误检查,但它对我有用.

As this is just a simple test, there is basically zero error checking, but it's working for me.

这篇关于在没有文件选择器的情况下读写硬盘上的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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