2

I want my code to rename files on my Desktop, but I do not have permission to edit files on my Desktop. How do I get the necessary permission?

My code:

public async Task RenameFile(fileLocation, newName)
{
    try
    {
        StorageFile bkpFile = await StorageFile.GetFileFromPathAsync(fileLocation);
        await bkpFile.RenameAsync(newName);
    }
    catch { /* Eat error */ }
}

Permissions set in my Package.Appxmanifest file:

<Capabilities>
  <Capability Name="picturesLibrary" />
  <Capability Name="documentsLibrary" />
  <Capability Name="internetClient" />
  <Capability Name="removableStorage" />
  <Capability Name="enterpriseAuthentication" />
  <Capability Name="videosLibrary" />
  <Capability Name="privateNetworkClientServer" />
  <DeviceCapability Name="location" />
  <DeviceCapability Name="webcam" />
  <DeviceCapability Name="microphone" />
  <m2:DeviceCapability Name="bluetooth.rfcomm">
    <m2:Device Id="any">
      <m2:Function Type="name:serialPort" />
    </m2:Device>
  </m2:DeviceCapability>
</Capabilities>

EDIT: I also need permission to rename files in attached devices, such as SD cards and thumb drives.

TS-
  • 317
  • 3
  • 15
  • Force the app to run in admin mode? http://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator – Mokey Feb 09 '17 at 18:14
  • I don't want them to have admin-level privileges, at least outside of this specific instance. – TS- Feb 09 '17 at 18:17
  • Try FileOPermission Class: https://msdn.microsoft.com/en-us/library/system.security.permissions.fileiopermission(v=vs.110).aspx – Mokey Feb 09 '17 at 19:12
  • I'm getting told that 'Permissions' does not exist in 'System.Security'. Does it matter that I'm using Win 8.1? – TS- Feb 09 '17 at 19:38
  • Assuming you're launching as a normal process, I don't think you'll get more inherent permissions than the user running the application? Are you asking how to prompt for elevated permissions? – Geoff Feb 10 '17 at 15:51
  • The user has access to their desktop (and external devices). My program is getting an "Access is Denied" error when trying to access these places the user can. The user can manual rename a file on their desktop, but my code cannot rename files on my desktop. – TS- Feb 10 '17 at 16:12

0 Answers0