如何删除,替换共享文件夹

时间:2017-09-25 11:18:07

标签: c#

您好我在共享文件夹中删除和替换文件时遇到问题 我有完全访问权限。

"拒绝访问路径"

复制功能在两个方向都很有效。 (但我必须手动删除文件)。

  • 服务器 - >>> PC
  • SERVER<<< - PC

    该程序连接到另一个用户下的文件服务器,并使用文件进行操作(复制文件工作正常,删除文件不起作用替换不起作用)。

    private void replaceFile(string source, string sourceName ,string destination)
        {
            try
            {
                using (new Impersonator(username(), domain(), password()))
                {
                    //File.delete(destination);
                    //File.Copy(source, destination, true);
    
                    File.Replace(source, destination, null);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    
    private void copyFile(string source, string destination)
              {
                  try
                  {
                      using (new Impersonator(username(), domain(), password()))
                      {
                          File.Copy(source, destination, true);
                      }
                  }
                  catch (Exception e)
                  {
                      MessageBox.Show(e.Message);
                  }
              }
    
    using System;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
    using System.Security.Principal;
    using System.Windows.Forms;
    
    namespace IFS_FileManager
    {
    
        public enum LogonType
        {
            LOGON32_LOGON_INTERACTIVE = 2,
            LOGON32_LOGON_NETWORK = 3,
            LOGON32_LOGON_BATCH = 4,
            LOGON32_LOGON_SERVICE = 5,
            LOGON32_LOGON_UNLOCK = 7,
            LOGON32_LOGON_NETWORK_CLEARTEXT = 8, // Win2K or higher
            LOGON32_LOGON_NEW_CREDENTIALS = 9 // Win2K or higher
        };
    
        public enum LogonProvider
        {
            LOGON32_PROVIDER_DEFAULT = 0,
            LOGON32_PROVIDER_WINNT35 = 1,
            LOGON32_PROVIDER_WINNT40 = 2,
            LOGON32_PROVIDER_WINNT50 = 3
        };
    
        public enum ImpersonationLevel
        {
            SecurityAnonymous = 0,
            SecurityIdentification = 1,
            SecurityImpersonation = 2,
            SecurityDelegation = 3
        }
    
        class Win32NativeMethods
        {
            [DllImport("advapi32.dll", SetLastError = true)]
            public static extern int LogonUser(string lpszUserName,
                 string lpszDomain,
                 string lpszPassword,
                 int dwLogonType,
                 int dwLogonProvider,
                 ref IntPtr phToken);
    
            [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern int DuplicateToken(IntPtr hToken,
                  int impersonationLevel,
                  ref IntPtr hNewToken);
    
    
    
            [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool RevertToSelf();
    
            [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
            public static extern bool CloseHandle(IntPtr handle);
        }
    
    
        public class Impersonator : IDisposable
        {
            private WindowsImpersonationContext _wic;
    
            public Impersonator(string userName, string domainName, string password, LogonType logonType, LogonProvider logonProvider)
            {
                Impersonate(userName, domainName, password, logonType, logonProvider);
            }
    
            public Impersonator(string userName, string domainName, string password)
            {
                Impersonate(userName, domainName, password, LogonType.LOGON32_LOGON_INTERACTIVE, LogonProvider.LOGON32_PROVIDER_DEFAULT);
            }
    
            public Impersonator() { }
    
    
            public void Dispose()
            {
                UndoImpersonation();
            }
    
            public void Impersonate(string userName, string domainName, string password)
            {
                Impersonate(userName, domainName, password, LogonType.LOGON32_LOGON_INTERACTIVE, LogonProvider.LOGON32_PROVIDER_DEFAULT);
            }
    
            public void Impersonate(string userName, string domainName, string password, LogonType logonType, LogonProvider logonProvider)
            {
                UndoImpersonation();
    
                IntPtr logonToken = IntPtr.Zero;
                IntPtr logonTokenDuplicate = IntPtr.Zero;
                try
                {
                    // revert to the application pool identity, saving the identity of the current requestor
                    _wic = WindowsIdentity.Impersonate(IntPtr.Zero);
    
                    // do logon & impersonate
                    if (Win32NativeMethods.LogonUser(userName,domainName,password,(int)logonType,(int)logonProvider,ref logonToken) != 0)
                    {
                        if (Win32NativeMethods.DuplicateToken(logonToken, (int)ImpersonationLevel.SecurityImpersonation, ref logonTokenDuplicate) != 0)
                        {
                            var wi = new WindowsIdentity(logonTokenDuplicate);
                            wi.Impersonate(); 
                            // discard the returned identity context (which is the context of the application pool)
                        }
                        else
                            MessageBox.Show("\n " + Marshal.GetLastWin32Error(), "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                        MessageBox.Show("\n " + Marshal.GetLastWin32Error(), "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (logonToken != IntPtr.Zero)
                        Win32NativeMethods.CloseHandle(logonToken);
    
                    if (logonTokenDuplicate != IntPtr.Zero)
                        Win32NativeMethods.CloseHandle(logonTokenDuplicate);
                }
            }
    
            /// <summary>
            /// Stops impersonation.
            /// </summary>
            private void UndoImpersonation()
            {
                // restore saved requestor identity
                if (_wic != null)
                    _wic.Undo();
                _wic = null;
            }
    
        }
    }
    

0 个答案:

没有答案