0

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

问题

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

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

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

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

(我有用户通过编程登录[查看登录用户功能])

我在 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 文件中 = 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)
      ...

请帮助和感谢。

4

0 回答 0