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?
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++;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With