thalassophile
thalassophile

Reputation: 275

How to store file into inetpub\wwwroot instead of local machine folder on UWP application

I am currently developing a UWP application for my school project and one of the pages allows the user to take a picture of themselves. I created the feature by following this tutorial: CameraStarterKit

For now I am storing the pictures taken on my desktop's picture folder. But the requirement of my project is to store the pictures taken in a folder called "Photos" under inetpub\wwwroot.

I dont really understand what wwwroot or IIS is... hence, I have no idea how I should modify my codes and store them into the folder.

Here are my codes for storing on my local desktop:

private async Task TakePhotoAsync()
    {
        idleTimer.Stop();
        idleTimer.Start();

        var stream = new InMemoryRandomAccessStream();

        //MediaPlayer mediaPlayer = new MediaPlayer();
        //mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/camera-shutter-click-03.mp3"));
        //mediaPlayer.Play();

        Debug.WriteLine("Taking photo...");
        await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);




        try
        {
            var file = await _captureFolder.CreateFileAsync("NYPVisitPhoto.jpg", CreationCollisionOption.GenerateUniqueName);
            Debug.WriteLine("Photo taken! Saving to " + file.Path);

            var photoOrientation = CameraRotationHelper.ConvertSimpleOrientationToPhotoOrientation(_rotationHelper.GetCameraCaptureOrientation());

            await ReencodeAndSavePhotoAsync(stream, file, photoOrientation);
            Debug.WriteLine("Photo saved!");


        }
        catch (Exception ex)
        {
            // File I/O errors are reported as exceptions
            Debug.WriteLine("Exception when taking a photo: " + ex.ToString());
        }



    }

For the storing of the files:

private static async Task ReencodeAndSavePhotoAsync(IRandomAccessStream stream, StorageFile file, PhotoOrientation photoOrientation)
    {
        using (var inputStream = stream)
        {
            var decoder = await BitmapDecoder.CreateAsync(inputStream);

            using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder);

                var properties = new BitmapPropertySet { { "System.Photo.Orientation", new BitmapTypedValue(photoOrientation, PropertyType.UInt16) } };

                await encoder.BitmapProperties.SetPropertiesAsync(properties);
                await encoder.FlushAsync();
            }
        }
    }

Upvotes: 2

Views: 1326

Answers (2)

kennyzx
kennyzx

Reputation: 12993

I would add an answer since there are tricky things about this requirement.

The first is the app can only access a few folders, inetpub is not one of them.

Using brokered Windows runtime component (I would suggest using FullTrustProcessLauncher, which is much simpler to develop and deploy) can enable UWP apps access folders in the same way as the traditional desktop applications do.

While this works for an ordinary folder, the inetpub folder, however, is different that it requires Administrators Privileges to write to, unless you turn UAC off.

The desktop component launched by the app does not have the adequate privileges to write to that folder, either.

So it think an alternative way would be setting up a virtual directory in IIS manager that maps to a folder in the public Pictures library, and the app saves picture to that folder.

From the website’s perspective, a virtual directory is the same as a real folder under inetpub, what differs is the access permissions.

Upvotes: 1

Ipsit Gaur
Ipsit Gaur

Reputation: 2927

Kennyzx is right here that you cannot access inetpub folder through your UWP application due to permissions.

But if your application fulfills following criteria then you can use Brokered Windows Component(a component within your app) to copy your file to any location in the system.

  1. Your application is a LOB application
  2. You are only targetting desktop devices(I assume this will be true because of your requirement)
  3. You are using side-loading for your app installation and distribution.

If all three are Yes then use Brokered Windows Component for UWP, it's not a small thing that can be showed here on SO using an example. So give worth a try reading and implementing it.

Upvotes: 0

Related Questions