简体   繁体   English

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

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

I have tried to copy image or file from computer to local folder in UWP Application but I have not done it.我试图将图像或文件从计算机复制到 UWP 应用程序中的本地文件夹,但我没有这样做。 I always told that file is access denied.我总是告诉文件被拒绝访问。 I searched on google and UWP sources in Microsoft website and I used most of the ways which I got from those links that I got.我在谷歌和微软网站上的 UWP 资源上进行了搜索,我使用了从这些链接中获得的大部分方法。 but I have not got it.但我没有得到它。

Thanks too much.太感谢了。 I found the solution and your answers are useful.我找到了解决方案,您的答案很有用。

this is the solution这是解决方案

This is the upload Button Image这是上传按钮图片

        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);
    }

retrun the path重新运行路径

    private static string GetPath(string folderName)
    {

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

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

This is a save button这是一个保存按钮

    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);
    }

By default, UWP apps do not have access to the file system, there are a couple ways to gain access while still giving the user control over the action.默认情况下,UWP 应用程序无权访问文件系统,有几种方法可以获得访问权,同时仍让用户控制操作。 The first one is through the file picker, you can save a token after the user picks a file so that you can access the file again in the future without asking the user.第一个是通过文件选择器,您可以在用户选择文件后保存一个令牌,以便您将来可以再次访问该文件而无需询问用户。 Another option is to use the BroadFileAccess capability in order to access files without asking the user to choose it in the file picker.另一种选择是使用 BroadFileAccess 功能来访问文件,而不要求用户在文件选择器中选择它。 Before this option works, the user will need to grant file access to the app from the Windows 10 privacy settings.在此选项生效之前,用户需要从 Windows 10 隐私设置授予对应用程序的文件访问权限。

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

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