繁体   English   中英

如何在UWP中的应用程序中将任意位置的图像或文件复制或删除到文件夹中

[英]how Copy or delete Image or file in any place to the Folder in the application in UWP

我试图将图像或文件从计算机复制到 UWP 应用程序中的本地文件夹,但我没有这样做。 我总是告诉文件被拒绝访问。 我在谷歌和微软网站上的 UWP 资源上进行了搜索,我使用了从这些链接中获得的大部分方法。 但我没有得到它。

太感谢了。 我找到了解决方案,您的答案很有用。

这是解决方案

这是上传按钮图片

        private async void BtnAddImage_OnClick(object sender, RoutedEventArgs e)
    {
        var imagePicker = new FileOpenPicker
        {
            ViewMode = PickerViewMode.Thumbnail,
            SuggestedStartLocation = PickerLocationId.Desktop,
            FileTypeFilter = { ".jpg", ".png", ".bmp", ".gif", ".tif" }
        };


        var imageFile = await imagePicker.PickSingleFileAsync();
        if (imageFile == null)
        {
            return;
        }

        _storageFile = imageFile;
        _path = imageFile.Path;

        var dataPackage = new DataPackage();

        dataPackage.SetBitmap(RandomAccessStreamReference.CreateFromFile(imageFile));
        Clipboard.SetContent(dataPackage);




        var dataPackageView = Clipboard.GetContent();
        if (!dataPackageView.Contains(StandardDataFormats.Bitmap)) return;


        IRandomAccessStreamReference imageReceived = await dataPackageView.GetBitmapAsync();


        if (imageReceived == null) return;
        using (var imageStream = await imageReceived.OpenReadAsync())
        {
            var bitmapImage = new BitmapImage();
            bitmapImage.SetSource(imageStream);
            ImgSupplier.Source = bitmapImage;

        }

        StorageApplicationPermissions.FutureAccessList.Add(imageFile);
    }

重新运行路径

    private static string GetPath(string folderName)
    {

  var root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path + @"\Assets\";

        var fullPath = Path.Combine(root, folderName);
        return fullPath;
    }

这是一个保存按钮

    private StorageFile _storageFile;   
    private async void BtnAddExpenditure_OnClick(object sender, RoutedEventArgs e)
    {
        var getPath = GetPath("Companies");

        var folder = await StorageFolder.GetFolderFromPathAsync(getPath);
        await _storageFile.CopyAsync(folder, Guid.NewGuid() + "." + _storageFile.FileType);
    }

默认情况下,UWP 应用程序无权访问文件系统,有几种方法可以获得访问权,同时仍让用户控制操作。 第一个是通过文件选择器,您可以在用户选择文件后保存一个令牌,以便您将来可以再次访问该文件而无需询问用户。 另一种选择是使用 BroadFileAccess 功能来访问文件,而不要求用户在文件选择器中选择它。 在此选项生效之前,用户需要从 Windows 10 隐私设置授予对应用程序的文件访问权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM