Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. var unc = new UNCAccess(clientpath, System.Configuration.ConfigurationManager.AppSettings["login"], "", System.Configuration.ConfigurationManager.AppSettings["password"]);
  2.  
  3.  
  4. // access your file system here
  5.  
  6.  
  7. unc.NetUseDelete();
  8.  
  9. using System;
  10. using System.Runtime.InteropServices;
  11. using BOOL = System.Boolean;
  12. using DWORD = System.UInt32;
  13. using LPWSTR = System.String;
  14. using NET_API_STATUS = System.UInt32;
  15.  
  16.  
  17. public class UNCAccess
  18. {
  19. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  20. internal struct USE_INFO_2
  21. {
  22. internal LPWSTR ui2_local;
  23. internal LPWSTR ui2_remote;
  24. internal LPWSTR ui2_password;
  25. internal DWORD ui2_status;
  26. internal DWORD ui2_asg_type;
  27. internal DWORD ui2_refcount;
  28. internal DWORD ui2_usecount;
  29. internal LPWSTR ui2_username;
  30. internal LPWSTR ui2_domainname;
  31. }
  32.  
  33. [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  34. internal static extern NET_API_STATUS NetUseAdd(
  35. LPWSTR UncServerName,
  36. DWORD Level,
  37. ref USE_INFO_2 Buf,
  38. out DWORD ParmError);
  39.  
  40. [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  41. internal static extern NET_API_STATUS NetUseDel(
  42. LPWSTR UncServerName,
  43. LPWSTR UseName,
  44. DWORD ForceCond);
  45.  
  46. private string sUNCPath;
  47. private string sUser;
  48. private string sPassword;
  49. private string sDomain;
  50. private int iLastError;
  51. public UNCAccess()
  52. {
  53. }
  54. public UNCAccess(string UNCPath, string User, string Domain, string Password)
  55. {
  56. login(UNCPath, User, Domain, Password);
  57. }
  58. public int LastError
  59. {
  60. get { return iLastError; }
  61. }
  62.  
  63. /// <summary>
  64. /// Connects to a UNC share folder with credentials
  65. /// </summary>
  66. /// <param name="UNCPath">UNC share path</param>
  67. /// <param name="User">Username</param>
  68. /// <param name="Domain">Domain</param>
  69. /// <param name="Password">Password</param>
  70. /// <returns>True if login was successful</returns>
  71. public bool login(string UNCPath, string User, string Domain, string Password)
  72. {
  73. sUNCPath = UNCPath;
  74. sUser = User;
  75. sPassword = Password;
  76. sDomain = Domain;
  77. return NetUseWithCredentials();
  78. }
  79. private bool NetUseWithCredentials()
  80. {
  81. uint returncode;
  82. try
  83. {
  84. USE_INFO_2 useinfo = new USE_INFO_2();
  85.  
  86. useinfo.ui2_remote = sUNCPath;
  87. useinfo.ui2_username = sUser;
  88. useinfo.ui2_domainname = sDomain;
  89. useinfo.ui2_password = sPassword;
  90. useinfo.ui2_asg_type = 0;
  91. useinfo.ui2_usecount = 1;
  92. uint paramErrorIndex;
  93. returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
  94. iLastError = (int)returncode;
  95. return returncode == 0;
  96. }
  97. catch
  98. {
  99. iLastError = Marshal.GetLastWin32Error();
  100. return false;
  101. }
  102. }
  103.  
  104. /// <summary>
  105. /// Closes the UNC share
  106. /// </summary>
  107. /// <returns>True if closing was successful</returns>
  108. public bool NetUseDelete()
  109. {
  110. uint returncode;
  111. try
  112. {
  113. returncode = NetUseDel(null, sUNCPath, 2);
  114. iLastError = (int)returncode;
  115. return (returncode == 0);
  116. }
  117. catch
  118. {
  119. iLastError = Marshal.GetLastWin32Error();
  120. return false;
  121. }
  122. }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement