4

I have created an application using Windows Universal Platform Tools in Visual Studio. In this application I have to rename files selected by user, but I am getting Permission denied exception while debugging.

After packing it & installing on machine I am not getting option to run it as administrator.

I have searched as much as I could but there doesn't seem to be any solution available on internet or I missed any query that could possibly solve this issue.

I can't see any type of permissions related to Storage as well in manifest file :(

Code: (Edit: This is now working Code)

            FolderPicker folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
        folderPicker.FileTypeFilter.Add("*");
        folderPicker.ViewMode = PickerViewMode.List;
        StorageFolder folderPicked = await folderPicker.PickSingleFolderAsync();

        if (folderPicked != null)
        {

            t_output.Text = folderPicked.Path.ToString();
            StringBuilder outputText = new StringBuilder();
            IReadOnlyList<StorageFile> fileList =
            await folderPicked.GetFilesAsync();
            int i=0;
             foreach (StorageFile file in fileList)
            {
                outputText.Append(file.Name + "\n");
                StorageFile fs = await folderPicked.GetFileAsync(file.Name);
                await fs.RenameAsync("tanitani0" + i + ".jpg");
                i++;

            }

I am using t_output.Text TextBox to verify each & everything is going as I expect, if I don't use File.Copy then every file is being listed from the selected folder just like I want. But getting Permission Denied issue with File.Copy :( if I directly Use File.Move I am getting File Not Found Exception then.

What can be the way to solve this type of problem?

Prince
  • 61
  • 1
  • 5
  • 2
    Anyone giving -1 if you could tell me the solution as well? :) – Prince Mar 21 '17 at 23:10
  • What files are you trying to rename? – NineBerry Mar 21 '17 at 23:15
  • NineBerry Sir, I am testing on image files, but any type of file will be renamed when user wants to i.e. text files, video files, etc. – Prince Mar 21 '17 at 23:17
  • Show the source code and also add the project settings of the uwp project – NineBerry Mar 21 '17 at 23:18
  • Possible duplicate of [Windows 10 Universal App File/Directory Access](http://stackoverflow.com/questions/33082835/windows-10-universal-app-file-directory-access) – NineBerry Mar 21 '17 at 23:21
  • I have uploaded the Code @NineBerry Sir, in the given link the answer says we can access files from the folder selected by user using FolderPicker. That's exactly what I want to do but still getting Permission Denied exception. – Prince Mar 21 '17 at 23:29
  • You don't specify the target folder for copying files – NineBerry Mar 21 '17 at 23:33
  • Thanks NineBerry Sir, I just tried giving target path as well, but still I am getting same permission denied exception; { File.Copy(file.Path, folderPicked.Path+@"\tani"+i+".jpg"); } { File.Copy(file.Path, folderPicked.Path+"tani"+i+".jpg"); } – Prince Mar 21 '17 at 23:35

1 Answers1

3

There are a few filesystem limitations in UWP, and you just ran into one of them. By getting access to the folder, you have to continue using that StorageFolder instance to make modifications.

To create a copy of the file, use folderPicked.CreateFileAsync(path) and use that StorageFile returned from that method to copy the stream data. But since you have access to the individual files inside the directory, you can take advantage of the StorageFile interface and perform your operations asynchronously. Here's a list of methods permitted:

https://learn.microsoft.com/en-us/uwp/api/windows.storage.storagefile

Using File.Copy(...) will only work in your isolated storage / application data directories. Just because you have folderPicked does not mean File.Copy(...) has access.

Code sample:

foreach (StorageFile file in fileList)
{
    outputText.Append(file.Name + "\n");
    int i = 0;

    await file.CopyAsync(pickedFolder, "tani" + i + ".jpg");
    i++;
}

On a side note, your int i value will always be zero. Just make sure it's outside the loop to continue incrementing.

int i = 0;

foreach (StorageFile file in fileList)
{
    outputText.Append(file.Name + "\n");        
    await file.CopyAsync(pickedFolder, "tani" + i + ".jpg");
    i++;
}
Laith
  • 6,071
  • 1
  • 33
  • 60
  • Thank you so much Laith Sir :D This worked ^_^ I created StorageFile object & renamed it using the builtin function :D (yes sir I noticed afterwards while rename method worked & I was getting error) – Prince Mar 22 '17 at 00:04