一个winform程序,用Directory.Delete(path)删除某个文件夹(文件夹是空的,不存在文件占用的问题),在其他系统下一切正常,但是在vista系统下抛出“Access to the path .... is denied”的异常。已经尝试过如下方法DirectorySecurity dSecurity = Directory.GetAccessControl(path);
①dSecurity.RemoveAccessRule(new FileSystemAccessRule(WindowsIdentity.GetCurrent().Name, FileSystemRights.FullControl, AccessControlType.Deny));
②dSecurity.RemoveAccessRule(new FileSystemAccessRule(WindowsIdentity.GetCurrent().Name, FileSystemRights.Delete, AccessControlType.Deny));
③ dSecurity.AddAccessRule(new FileSystemAccessRule(WindowsIdentity.GetCurrent().Name, FileSystemRights.FullControl, AccessControlType.Allow));
④dSecurity.AddAccessRule(new FileSystemAccessRule(WindowsIdentity.GetCurrent().Name, FileSystemRights.Delete, AccessControlType.Allow));
Directory.SetAccessControl(path, dSecurity);还是不行。请问有什么方法可以解决?

解决方案 »

  1.   


    这个我也想过,把程序的运行环境设成管理员。但是这样的话每次程序启动都会弹出UAC的对话框提醒用户,这样感觉不是很好。我只是想删除My Document下面的一个文件夹而已啊。
      

  2.   

    在VISTA里是没有办法,除非关了UAC就可以了,
      

  3.   

    新建应用程序配置文件 里面有你想要的 <?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="MyApplication.app" />
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
          <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <!-- UAC 清单选项
                如果要更改 Windows 用户帐户控制级别,请用以下节点之一替换 
                requestedExecutionLevel 节点。        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
            <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
            <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />            指定 requestedExecutionLevel 节点将会禁用文件和注册表虚拟化。
                如果要利用文件和注册表虚拟化实现向后 
                兼容性,则删除 requestedExecutionLevel 节点。
            -->
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
          </requestedPrivileges>
          <applicationRequestMinimum>
            <defaultAssemblyRequest permissionSetReference="Custom" />
            <PermissionSet ID="Custom" SameSite="site" />
          </applicationRequestMinimum>
        </security>
      </trustInfo>
      <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
          <!-- 此应用程序设计使用的所有 Windows 版本的列表。Windows 将会自动选择最兼容的环境。-->
          <!-- 如果应用程序设计使用 Windows 7,请取消注释以下 supportedOS 节点-->
          <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>-->
        </application>
      </compatibility>
      <!-- 启用 Windows 公共控件和对话框的主题(Windows XP 和更高版本) -->
      <!-- <dependency>
        <dependentAssembly>
          <assemblyIdentity
              type="win32"
              name="Microsoft.Windows.Common-Controls"
              version="6.0.0.0"
              processorArchitecture="*"
              publicKeyToken="6595b64144ccf1df"
              language="*"
            />
        </dependentAssembly>
      </dependency>-->
    </asmv1:assembly>