首页 文章

Directory.GetDirectories在异步任务操作中返回空字符串

提问于
浏览
0

我有一个UWP应用程序,用于捕获和处理来自摄像头的图像 . 该项目利用Microsoft认知服务人脸识别API,我现在正在探索应用程序的现有功能 . 我的目标是当相机识别人物的图像时(通过脸部识别API服务),我想显示该人的相关图像 .

这样,图像被捕获并存储在我的机器的本地目录中 . 我想检索图像文件并在识别出人物后在屏幕上呈现它 .

下面的代码显示 async 任务方法 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 类但不在 async 任务方法中 . 使用 async 时是否导致在使用I / O操作时未返回文件?

抱歉这个愚蠢的问题,但我真的不明白 .

任何帮助是极大的赞赏 .

1 回答

  • 5

    使用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
            }
    

相关问题