问题
My application creates a log file when it connects or disconnects a server. This works well win win XP.
There are no issues while I run it as admin in Vista and Win 7. But if I run this as a standard user in Vista or Win7 then it fails to create the log file, and I know this is for UAC.
But how can I bypass this situation so that my app will run as admin . DO I need to add any registry entry while installing my application so that it will always run as administrator.
回答1:
If you really want to, you can add a manifest that causes your application to require UAC elevation when it is launched, but more often than not that's not what you should do.
More likely, you're writing to a path you shouldn't be. User mode applications should only write to the current users folders or the C:\Users\Public
folder, and things like logs should go in C:\Users\Current User Name\Application Data\Your Program Name\
. To get the ApplicationData path use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
. Or if you want to have the file shared by all users, use Environment.SpecialFolder.CommonApplicationData
.
来源:https://stackoverflow.com/questions/4487517/unable-to-create-or-write-to-a-file-as-standard-user