I have a program that the user can save the output session to a text file. This works fine, but when I try to save the file to a location such as C:\, it shows an error dialog saying:
You don't have permission to save to this location. Contact the administrator to obtain permission. Would you like to save to the Documents folder instead?
When I click no, thinking it would just cancel the operation, my application crashes.
I do not want my application to require administrative privileges, but how, when attempting to save the file, do I ask the user to provide privileges (such as bring up the UAC dialog, and if required provide authentication). If the user cannot provide the permissions I would like to just stay on the Save File Dialog until the user either picks a location they have permission to save to, or presses cancel. I do not really want the permission to be asked for when the program starts, just when the user saves the file to a location they need permission for.
By the way I am currently logged in as administrator when running my application, it’s the only account on my computer running Windows Vista.
Applications elevate only at startup. To simulate “getting UAC privs while running and only if needed” you actually need to partition your app so that it launches a second process (for example, to write to the file) and this process might be launched elevated. You can do this, but it’s a lot of work for little or no benefit.
The whole point of UAC is to change the way users and applications treat the hard drive. The root of C is not a smart place to write things. Use AppData, or Documents, or create your own folder like C:\MySpecialApp (add the path to a config file) and write there. If you insist on writing to the root of C, then you require admin privs which doesn’t make much sense.