c# 访问局域网共享盘文件【转载】

  1. 转载自:http://www.guaik.com/cs/SOAREDF1IAIY7V2A.shtm
  2. public class IdentityScope : IDisposable   
  3.     {   
  4.         // obtains user token   
  5.         [DllImport("advapi32.dll", SetLastError = true)]   
  6.         static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,   
  7.             int dwLogonType, int dwLogonProvider, ref IntPtr phToken);   
  8.   
  9.         // closes open handes returned by LogonUser   
  10.         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]   
  11.         extern static bool CloseHandle(IntPtr handle);   
  12.   
  13.         [DllImport("Advapi32.DLL")]   
  14.         static extern bool ImpersonateLoggedOnUser(IntPtr hToken);   
  15.   
  16.         [DllImport("Advapi32.DLL")]   
  17.         static extern bool RevertToSelf();   
  18.         const int LOGON32_PROVIDER_DEFAULT = 0;   
  19.         const int LOGON32_LOGON_NEWCREDENTIALS = 9;//域控中的需要用:Interactive = 2   
  20.         private bool disposed;   
  21.         public IdentityScope(string sUsername, string sDomain, string sPassword)   
  22.         {   
  23.             // initialize tokens   
  24.             IntPtr pExistingTokenHandle = new IntPtr(0);   
  25.             IntPtr pDuplicateTokenHandle = new IntPtr(0);   
  26.   
  27.             try  
  28.             {   
  29.                 // get handle to token   
  30.                 bool bImpersonated = LogonUser(sUsername, sDomain, sPassword,   
  31.                     LOGON32_LOGON_NEWCREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle);   
  32.   
  33.                 if (true == bImpersonated)   
  34.                 {   
  35.                     if (!ImpersonateLoggedOnUser(pExistingTokenHandle))   
  36.                     {   
  37.                         int nErrorCode = Marshal.GetLastWin32Error();   
  38.                         throw new Exception("ImpersonateLoggedOnUser error;Code=" + nErrorCode);   
  39.                     }   
  40.                 }   
  41.                 else  
  42.                 {   
  43.                     int nErrorCode = Marshal.GetLastWin32Error();   
  44.                     throw new Exception("LogonUser error;Code=" + nErrorCode);   
  45.                 }   
  46.             }   
  47.             finally  
  48.             {   
  49.                 // close handle(s)   
  50.                 if (pExistingTokenHandle != IntPtr.Zero)   
  51.                     CloseHandle(pExistingTokenHandle);   
  52.                 if (pDuplicateTokenHandle != IntPtr.Zero)   
  53.                     CloseHandle(pDuplicateTokenHandle);   
  54.             }   
  55.         }   
  56.   
  57.         protected virtual void Dispose(bool disposing)   
  58.         {   
  59.             if (!disposed)   
  60.             {   
  61.                 RevertToSelf();   
  62.                 disposed = true;   
  63.             }   
  64.         }   
  65.   
  66.         public void Dispose()   
  67.         {   
  68.             Dispose(true);   
  69.         }   
  70.     }  


使用也很简单:

  1. using (IdentityScope iss = new IdentityScope("userName""HostIp""password"))   
  2. {   
  3.        File.Copy("C:\\store_schema.sql""\\\\192.168.0.111\\Share\\a.sql");   
  4. }  
posted @ 2011-04-21 08:27  中华  阅读(2173)  评论(0)    收藏  举报