简体   繁体   中英

c# UWP Getting access to path denied when writing bytes to a new file after array created by DataReader

I set file permission of directory Everyone full control, but still get error.

THE ERROR 在此处输入图片说明

THE CODE

        /// Handles a frame arrived event and renders the frame to the screen.
    /// </summary>
    private void FrameReader_FrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
    {
        // TryAcquireLatestFrame will return the latest frame that has not yet been acquired.
        // This can return null if there is no such frame, or if the reader is not in the
        // "Started" state. The latter can occur if a FrameArrived event was in flight
        // when the reader was stopped.

        // Get frame image from camera:
        using (MediaFrameReference  frame = sender.TryAcquireLatestFrame())   //  ==>  MediaFrameReference 
        {
            // Got image?
            if (frame != null)
            {
                                    // DIAG: Save image data to file for analysis:
                                        // Get image byte data: 
                                            DataReader dataReader = DataReader.FromBuffer( frame.BufferMediaFrame.Buffer );
                                            byte[] camera_image_bytes = new byte[ frame.BufferMediaFrame.Buffer.Length];
                                            dataReader.ReadBytes( camera_image_bytes );

                                        // Save to file :
                                            var len =  camera_image_bytes.Length;
                                            System.IO.File.WriteAllBytes( "C:\\PRIMARY\\WORK\\CAMERA READER\\camera_image_bytes.bin", camera_image_bytes );  <<<<<<<<<<<   E R R O R   H E R E

The error is correct: your app doesn't have access to C:\\PRIMARY\\WORK\\CAMERA READER\\ . UWP apps run with a subset of the user's permissions and by default can directly write only to their application data folders. See the UWP File Access Permissions documentation for more details.

Your user can grant the app brokered access to other areas via several means, most likely being the FileSavePicker (to let the user pick where to save) or declared capabilities such as picturesLibrary or broadFileSystemAccess. See Accessing additional locations These methods will allow the app to access the folder via the file broker and the StorageFile and StorageFolder objects.

From what you present here that should be sufficient. Windows.Storage.FileIO.WriteBytesAsync is very similar to your System.IO.File.WriteAllBytes call. If you need to use System.IO instead of Windows.Storage you can get a brokered HANDLE via IStorageItemHandleAccess and then use that to initialize System.IO.FileStreams and such.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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