简体   繁体   中英

WPF Application not getting file access rights

I have a WPF app that creates some text files in its own install directory. However, even after the UAC prompt, windows vista and windows 7 users often times still get "file access failed" type errors. The solution is to find the executable in windows explorer and open up the compatibility tab under the file properties and check "run as administrator". This is obviously a terrible user experience but I'm not sure how to ensure the app can secure itself these permissions without that step being taken. I am not trying to bypass the UAC prompts.

In general, .Net wants you to put application generated files either into the user's home directory or the shared user folder. Have a look at this answer: When using a Settings.settings file in .NET, where is the config actually stored? It talks about .Net config files but you can put other files there.

You can force your app to start with admin rights (UAC will show it's dialog box anyway) by embedding custom manifest (project properties -> build -> Manifest).

Manifest example ( requestedExecutionLevel part is importaint):

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0"
  xmlns="urn:schemas-microsoft-com:asm.v1"
  xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
  xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <assemblyIdentity version="1.0.0.0" name="yourappname.app"/>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
            <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
            </requestedPrivileges>
        </security>
    </trustInfo>
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
            <!-- A list of all Windows versions that this application is designed to work with. Windows will automatically select the most compatible environment.-->
            <!-- If your application is designed to work with Windows 7, uncomment the following supportedOS node-->
            <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>-->
        </application>
    </compatibility>
</asmv1:assembly>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM