关于UnauthorizedAccessException的C#错误

时间:2013-07-17 08:53:43

标签: c# networking copy acl

我想将文件复制到网络驱动器。但是它显示错误:C:\ pdf.zip有denide(copytonetworkingdrive函数)。 所以我想更改文件ACL(copyfileto function)。

问题

1。)我无法访问c:\ pdf.zip

2.。)我无法将文件c:\复制到网络驱动器。

3.。)我无法更改文件访问控制。

4.。我无法访问\\ networkingdrive \\ blablabla $

(我已通过编程登录[查看logonuser功能])

我开发的是Windows 7 64位,VS C#2012,.Net Framework 4.0。

using Ionic.Zip;
using System.Security.Principal;
using System.Security.Permissions;
using System.Security.AccessControl;
using System.Management;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

我的代码:

private static void copyfileto(string fin, string fout)
{
       int i;
       FileStream fsin, fsout;
       *FileSecurity files = File.GetAccessControl(fin);*
       WindowsIdentity self = System.Security.Principal.WindowsIdentity.GetCurrent();
       FileSystemAccessRule rule = new FileSystemAccessRule( self.Name, FileSystemRights.FullControl,AccessControlType.Allow);

       AuthorizationRuleCollection acl = files.GetAccessRules(true,true, typeof(System.Security.Principal.NTAccount));
       files.AddAccessRule( rule );

       fsin = new FileStream( fin ,FileMode.Open);
       fsout = new FileStream( fout ,FileMode.Create);

       do 
       { i = fsin.ReadByte() ;
         if ( i != -1 ) fsout.WriteByte((byte) i );
       } while ( i != -1);

       fsin.Close();
       fsout.Close();

}



 private void copytonetworkdrive()
   {
      AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
      IntPtr hTokenLocal;
      if (LogonUser(frm_main.myapp.gl_login_user_for_attachment.ToString(),
          frm_main.myapp.gl_folder_attachment_server.ToString(),
          frm_main.myapp.gl_pass_user_for_attachment.ToString(), LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, out hTokenLocal))
      {

         //if (hToken != IntPtr.Zero) CloseHandle(hToken);
         //if (hTokenDuplicate != IntPtr.Zero) CloseHandle(hTokenDuplicate);
         WindowsIdentity identity = new WindowsIdentity(hTokenLocal);
         WindowsImpersonationContext context = identity.Impersonate();

         copyfileto("C:\\Pdf.zip",frm_main.myapp.gl_folder_attachment_server.ToString() +@"ZIP\quotation\Pdf.zip");
      }
   }

FileSecurity files = File.GetAccessControl(fin); 中 错误:

System.UnauthorizedAccessException was unhandled
  HResult=-2147024891
  Message=Attempted to perform an unauthorized operation.
  Source=mscorlib
  StackTrace:
       at System.Security.AccessControl.Win32.GetSecurityInfo(ResourceType resourceType, String name, SafeHandle handle, AccessControlSections accessControlSections, RawSecurityDescriptor& resultSd)
       at System.Security.AccessControl.NativeObjectSecurity.CreateInternal(ResourceType resourceType, Boolean isContainer, String name, SafeHandle handle, AccessControlSections includeSections, Boolean createByName, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext)
       at System.Security.AccessControl.FileSystemSecurity..ctor(Boolean isContainer, String name, AccessControlSections includeSections, Boolean isDirectory)
      ...

请帮助,谢谢。

0 个答案:

没有答案