zoukankan      html  css  js  c++  java
  • UWP中的文件相关操作

    最近开始做UWP开发,图省事儿就把自己之前一个Winform项目的一部分代码拷贝到了新写的UWP项目中来。整出了一些幺蛾子,下面做一个记录。

    首先提一个重点就是:UWP里关于文件的操作尽量用StorageFile类来搞!!!!!!!!!!!!

    1.UWP的文件选取

    UWP的文件选取使用的是FileOpenPicker,我这里是用来选图片文件的,不多说直接上代码:

                FileOpenPicker fileOpenPicker = new FileOpenPicker();
                fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                fileOpenPicker.FileTypeFilter.Add(".jpg");
                fileOpenPicker.FileTypeFilter.Add(".png");
                fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
    
                var imgFile = await fileOpenPicker.PickSingleFileAsync();
    
                if (imgFile == null)
                {
                    return;
                }

    关于这块儿的具体的各种操作可以去微软爸爸那里查,最标准最权威:https://docs.microsoft.com/zh-cn/windows/uwp/audio-video-camera/imaging

    2.文件读取操作

    这一块儿的BUG是最让我恶心的!!

    最开始的时候这一块儿的代码我是直接从Winform项目里直接拷出来的用的是File.ReadAllBytes,结果Debug的时候什么问题都没有出现,Release出来后直接提示我没有权限访问文件(UnauthorizedAccessException)。。。。。

    最初的错误代码(可以在Winform里面用,UWP里面的话Release出来跑不成):

    private async Task<List<ByteArrayContent>> GetFileByteArrayContent(HashSet<string> files)
            {
                List<ByteArrayContent> list = new List<ByteArrayContent>();
               
                foreach (var file in files)
                {
                    await Task.Run(() => {
                        if(file.Length > 0)
                        {
                            try
                            {
                                //file是string类型的文件路径
                                var fileContent = new ByteArrayContent(File.ReadAllBytes(file));
                                ContentDispositionHeaderValue dispositionHeader = new ContentDispositionHeaderValue("file");
                                dispositionHeader.DispositionType = "file";
                                dispositionHeader.Name = "imageFile";
                                dispositionHeader.FileName = Path.GetFileName(file);
                                fileContent.Headers.ContentDisposition = dispositionHeader;
                                list.Add(fileContent);
                            }
                            catch(Exception ex)
                            {
                                this.TextBlock_lyric.Text = ex.Message;
                            }
                        }
                    });
                }
                return list;
            }

    然后我去微软爸爸那儿里查了一下File.ReadAllBytes函数https://msdn.microsoft.com/en-us/library/system.io.file.readallbytes(v=vs.110).aspx之后发现问题原因应该是没有权限访问文件,查到问题所在后就开始用StorageFile的方法来处理自己所选择的文件修改后的代码如下:

            private async Task<List<ByteArrayContent>> GetByteArrayContents()
            {
                List<ByteArrayContent> files = new List<ByteArrayContent>();
                string exceptionMsg = string.Empty;
                if (imgFile != null)
                {
                    try
                    {
                        //imgFile是一个StorageFile类的对象
                        var buffer = await FileIO.ReadBufferAsync(imgFile);
                        byte[] content = new byte[buffer.Length];
                        // Use a dataReader object to read from the buffer
                        using (DataReader dataReader = DataReader.FromBuffer(buffer))
                        {
                            dataReader.ReadBytes(content);
                            // Perform additional tasks
                        }
    
                        var fileContent = new ByteArrayContent(content);
                        ContentDispositionHeaderValue dispositionHeader = new ContentDispositionHeaderValue("file");
                        dispositionHeader.DispositionType = "file";
                        dispositionHeader.Name = "imageFile";
                        dispositionHeader.FileName = imgFile.Path;
                        fileContent.Headers.ContentDisposition = dispositionHeader;
                        files.Add(fileContent);
                    }
                    catch (Exception ex)
                    {
                        exceptionMsg = ex.Message;
                    }
                }
                this.TextBlock_lyric.Text += exceptionMsg;
                return files;
            }

    3.其他

    更改控件属性的操作不能写到异步操作里,不然会崩

    程序里有读取文件的操作的话尽量去把Package.appxmanifest文件里对应的权限开一下,虽然有的人说不开也行。。。但是我头不铁,我还是老老实实开了。

    问题解决后总结出一条经验,MSDN真好用!!

      

  • 相关阅读:
    Codeforces Round #551 (Div. 2) F. Serval and Bonus Problem (DP/FFT)
    Codeforces Round #551 (Div. 2) E. Serval and Snake (交互题)
    BZOJ 5495: [2019省队联测]异或粽子 (trie树)
    洛谷【P2669】NOIP2015普及组 T1金币
    解决Win 10上SSD缓慢问题
    如何保障数据安全
    一个网工的linux学习过程
    JS实现select去除option的使用注意事项
    codevs1506传话(kosaraju算法)
    我的园子
  • 原文地址:https://www.cnblogs.com/Mr-Owl/p/8874200.html
Copyright © 2011-2022 走看看