daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Oct 23rd, 2017 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.ComponentModel;
  3. using System.Net;
  4. using System.Runtime.InteropServices;
  5. using System.Runtime.Versioning;
  6. using System.Security.AccessControl;
  7. using BOOL = System.Boolean;
  8. using DWORD = System.UInt32;
  9. using LPWSTR = System.String;
  10. using NET_API_STATUS = System.UInt32;
  11.  
  12. namespace Integrations.Packing_Slips
  13. {
  14.  
  15.     public class UNCAccess : IDisposable
  16.     {
  17.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  18.         internal struct USE_INFO_2
  19.         {
  20.             internal LPWSTR ui2_local;
  21.             internal LPWSTR ui2_remote;
  22.             internal LPWSTR ui2_password;
  23.             internal DWORD ui2_status;
  24.             internal DWORD ui2_asg_type;
  25.             internal DWORD ui2_refcount;
  26.             internal DWORD ui2_usecount;
  27.             internal LPWSTR ui2_username;
  28.             internal LPWSTR ui2_domainname;
  29.         }
  30.         [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  31.         internal static extern NET_API_STATUS NetUseAdd(
  32.         LPWSTR UncServerName,
  33.         DWORD Level,
  34.         ref USE_INFO_2 Buf,
  35.         out DWORD ParmError);
  36.         [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  37.         internal static extern NET_API_STATUS NetUseDel(
  38.         LPWSTR UncServerName,
  39.         LPWSTR UseName,
  40.         DWORD ForceCond);
  41.         private string sUNCPath;
  42.         private string sUser;
  43.         private string sPassword;
  44.         private string sDomain;
  45.         private int iLastError;
  46.         public UNCAccess()
  47.         {
  48.         }
  49.         public UNCAccess(string UNCPath, string User, string Domain, string Password)
  50.         {
  51.             login(UNCPath, User, Domain, Password);
  52.         }
  53.         public int LastError
  54.         {
  55.             get { return iLastError; }
  56.         }
  57.         ///
  58.  
  59.         /// Connects to a UNC share folder with credentials
  60.         ///
  61.         /// UNC share path
  62.         /// Username
  63.         /// Domain
  64.         /// Password
  65.         /// True if login was successful
  66.         public bool login(string UNCPath, string User, string Domain, string Password)
  67.         {
  68.             sUNCPath = UNCPath;
  69.             sUser = User;
  70.             sPassword = Password;
  71.             sDomain = Domain;
  72.             return NetUseWithCredentials();
  73.         }
  74.         private bool NetUseWithCredentials()
  75.         {
  76.             uint returncode;
  77.             try
  78.             {
  79.                 USE_INFO_2 useinfo = new USE_INFO_2();
  80.  
  81.                 useinfo.ui2_remote = sUNCPath;
  82.                 useinfo.ui2_username = sUser;
  83.                 useinfo.ui2_domainname = sDomain;
  84.                 useinfo.ui2_password = sPassword;
  85.                 useinfo.ui2_asg_type = 0;
  86.                 useinfo.ui2_usecount = 1;
  87.                 uint paramErrorIndex;
  88.                 returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
  89.                 iLastError = (int)returncode;
  90.                 return returncode == 0;
  91.             }
  92.             catch
  93.             {
  94.                 iLastError = Marshal.GetLastWin32Error();
  95.                 return false;
  96.             }
  97.         }
  98.         ///
  99.  
  100.         /// Closes the UNC share
  101.         ///
  102.         /// True if closing was successful
  103.         public bool NetUseDelete()
  104.         {
  105.             uint returncode;
  106.             try
  107.             {
  108.                 returncode = NetUseDel(null, sUNCPath, 2);
  109.                 iLastError = (int)returncode;
  110.                 return (returncode == 0);
  111.             }
  112.             catch
  113.             {
  114.                 iLastError = Marshal.GetLastWin32Error();
  115.                 return false;
  116.             }
  117.         }
  118.  
  119.         public void Dispose()
  120.         {
  121.             NetUseDelete();
  122.             GC.SuppressFinalize(this);
  123.         }
  124.     }
  125.  
  126. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top