【问题标题】:Directory.GetDirectories return empty string inside an async Task operationDirectory.GetDirectories 在异步任务操作中返回空字符串
【发布时间】:2017-03-27 16:54:31
【问题描述】:

我有一个 UWP 应用程序,用于捕获和处理来自相机的图像。该项目利用 Microsoft Cognitive Services Face Recognition API,我正在探索该应用程序的现有功能一段时间。我的目标是当一个人的图像被相机识别(通过人脸识别 API 服务)时,我想显示那个人的相关图像。

这样,图像就会被捕获并存储在我机器的本地目录中。一旦识别出此人,我想检索图像文件并将其呈现在屏幕上。

下面的代码展示了asyncTask方法ProcessCameraCapture

private async Task ProcessCameraCapture(ImageAnalyzer e)
    {
        if (e == null)
        {
            this.UpdateUIForNoFacesDetected();
            this.isProcessingPhoto = false;
            return;
        }

        DateTime start = DateTime.Now;

        await e.DetectFacesAsync();

        if (e.DetectedFaces.Any())
        {
            string names;
            await e.IdentifyFacesAsync();

            this.greetingTextBlock.Text = this.GetGreettingFromFaces(e, out names);

            if (e.IdentifiedPersons.Any())
            {
                this.greetingTextBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.GreenYellow);
                this.greetingSymbol.Foreground = new SolidColorBrush(Windows.UI.Colors.GreenYellow);
                this.greetingSymbol.Symbol = Symbol.Comment;

                GetSavedFilePhoto(names);
            }
            else
            {
                this.greetingTextBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.Yellow);
                this.greetingSymbol.Foreground = new SolidColorBrush(Windows.UI.Colors.Yellow);
                this.greetingSymbol.Symbol = Symbol.View;
            }
        }
        else
        {
            this.UpdateUIForNoFacesDetected();
        }

        TimeSpan latency = DateTime.Now - start;
        this.faceLantencyDebugText.Text = string.Format("Face API latency: {0}ms", (int)latency.TotalMilliseconds);

        this.isProcessingPhoto = false;
    }

GetSavedFilePhoto 中,一旦识别出此人,我就传递了字符串名称参数。

GetSavedFilePhoto 方法的代码如下

private void GetSavedFilePhoto(string personName)
    {
        if (string.IsNullOrWhiteSpace(personName)) return;

        var directoryPath = @"D:\PersonImages";

        var directories = Directory.GetDirectories(directoryPath);
        var filePaths = Directory.GetFiles(directoryPath, "*.jpg", SearchOption.AllDirectories);
    }

但是,在GetSavedFilePhoto 方法中,变量directories 在使用directoryPath 字符串变量时返回了一个空的数组字符串。目录“D:\PersonImages”是我机器中一个有效且现有的文件夹,它包含内部带有图像的子文件夹。我还尝试Directory.GetFiles 检索 jpg 图像,但仍然返回一个空字符串。

我认为它应该可以工作,因为我已经多次使用 Directory 类,但没有在 asyncTask 方法中使用。使用async会不会导致使用I/O操作时文件不返回?

对不起这个愚蠢的问题,但我真的不明白。

非常感谢任何帮助。

【问题讨论】:

  • 您的应用程序是否有权读取该文件?它在异步方法中不起作用吗?
  • @VMAtm 在async 方法中不起作用。我想是的,我在我的 Visual Studio 中调试,我发现这个方法Directory.GetDirectories(directoryPath) 也返回了一个空字符串Directory.GetFiles()。我创建了一个单独的控制台应用程序,只是为了检查我是否在使用 Directory 类时做错了,但在控制台应用程序中 GetDirectoriesGetFiles 返回了文件夹的数组字符串。我认为这与async Task 操作有关。你知道如何解决这个问题吗?
  • 在 UWP 中你不能像在 var directoryPath = @"D:\PersonImages"; 中那样随机访问文件夹你需要文件夹选择器或使用 PictyreLibrary

标签: c# uwp async-await


【解决方案1】:

使用Directory.GetFilesDirectory.GetDirectories方法可以通过以下代码获取Application本地文件夹中的文件夹/文件。但是打不开D:\

var directories = Directory.GetDirectories(ApplicationData.Current.LocalFolder.Path);

在UWP应用中默认只能访问两个位置(本地文件夹和安装文件夹),其他需要功能设置或file open picker。详情请参考file access permission

如果您需要访问D:\ 中的所有文件,用户必须使用FolderPicker 手动选择D:\ 驱动器,然后您有权访问此驱动器中的文件。

var picker = new Windows.Storage.Pickers.FileOpenPicker();
    picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
    picker.SuggestedStartLocation =
    Windows.Storage.Pickers.PickerLocationId.ComputerFolder;
    picker.FileTypeFilter.Add(".jpg");
    picker.FileTypeFilter.Add(".jpeg");
    picker.FileTypeFilter.Add(".png");

Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
        if (file != null)
        {
            // Application now has read/write access to the picked file

        }
        else
        {
           //do some stuff
        }

【讨论】:

  • 朱,谢谢你的回答。我按照您的 UWP 示例代码进行了操作,并且成功了。我现在可以访问 D:\ 并获取其中的子文件夹。
猜你喜欢
  • 2015-02-18
  • 2013-04-16
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 2016-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多