Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. /// <summary>
  2. /// Helper methods for mounting a UNC network share
  3. /// </summary>
  4. public class UncAccess
  5. {
  6. /// <summary>The domain.</summary>
  7. private string domain;
  8.  
  9. /// <summary>The last error.</summary>
  10. private int lastError;
  11.  
  12. /// <summary>The password.</summary>
  13. private string password;
  14.  
  15. /// <summary>The unc path.</summary>
  16. private string uncPath;
  17.  
  18. /// <summary>The user.</summary>
  19. private string user;
  20.  
  21. /// <summary>Initialises a new instance of the <see cref="UncAccess"/> class.</summary>
  22. public UncAccess()
  23. {
  24. }
  25.  
  26. /// <summary>Initialises a new instance of the <see cref="UncAccess"/> class.</summary>
  27. /// <param name="uncPath">The unc path.</param>
  28. /// <param name="user">The user.</param>
  29. /// <param name="domain">The domain.</param>
  30. /// <param name="password">The password.</param>
  31. public UncAccess(string uncPath, string user, string domain, string password)
  32. {
  33. this.Login(uncPath, user, domain, password);
  34. }
  35.  
  36. /// <summary>Gets the last error.</summary>
  37. public int LastError
  38. {
  39. get
  40. {
  41. return this.lastError;
  42. }
  43. }
  44.  
  45. /// <summary>
  46. /// Closes the UNC share
  47. /// </summary>
  48. /// <returns>True if closing was successful</returns>
  49. public bool NetUseDelete()
  50. {
  51. try
  52. {
  53. var returncode = NetUseDel(null, this.uncPath, 2);
  54. this.lastError = (int)returncode;
  55. return returncode == 0;
  56. }
  57. catch
  58. {
  59. this.lastError = Marshal.GetLastWin32Error();
  60. return false;
  61. }
  62. }
  63.  
  64. /// <summary>Connects to a UNC share folder with credentials</summary>
  65. /// <param name="uncPath">The unc Path.</param>
  66. /// <param name="user">The user.</param>
  67. /// <param name="domain">The domain.</param>
  68. /// <param name="password">The password.</param>
  69. /// <returns>True if login was successful</returns>
  70. public bool Login(string uncPath, string user, string domain, string password)
  71. {
  72. this.uncPath = uncPath;
  73. this.user = user;
  74. this.password = password;
  75. this.domain = domain;
  76. return this.NetUseWithCredentials();
  77. }
  78.  
  79. /// <summary>The net use add.</summary>
  80. /// <param name="uncServerName">The unc server name.</param>
  81. /// <param name="level">The level.</param>
  82. /// <param name="buf">The buf.</param>
  83. /// <param name="parmError">The parm error.</param>
  84. /// <returns>The <see cref="uint"/>.</returns>
  85. [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  86. internal static extern uint NetUseAdd(string uncServerName, uint level, ref UseInfo2 buf, out uint parmError);
  87.  
  88. /// <summary>The net use del.</summary>
  89. /// <param name="uncServerName">The unc server name.</param>
  90. /// <param name="useName">The use name.</param>
  91. /// <param name="forceCond">The force cond.</param>
  92. /// <returns>The <see cref="uint"/>.</returns>
  93. [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  94. internal static extern uint NetUseDel(string uncServerName, string useName, uint forceCond);
  95.  
  96. /// <summary>The net use with credentials.</summary>
  97. /// <returns>The <see cref="bool"/>.</returns>
  98. private bool NetUseWithCredentials()
  99. {
  100. try
  101. {
  102. var useinfo = new UseInfo2
  103. {
  104. ui2_remote = this.uncPath, ui2_username = this.user, ui2_domainname = this.domain, ui2_password = this.password,
  105. ui2_asg_type = 0, ui2_usecount = 1
  106. };
  107.  
  108. uint paramErrorIndex;
  109. var returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
  110. this.lastError = (int)returncode;
  111. return returncode == 0;
  112. }
  113. catch
  114. {
  115. this.lastError = Marshal.GetLastWin32Error();
  116. return false;
  117. }
  118. }
  119.  
  120. /// <summary>The use info 2.</summary>
  121. [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore", Justification = "Reviewed. Suppression is OK here.")]
  122. [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
  123. Justification = "Reviewed. Suppression is OK here.")]
  124. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  125. internal struct UseInfo2
  126. {
  127. /// <summary>The ui 2_local.</summary>
  128. internal string ui2_local;
  129.  
  130. /// <summary>The ui 2_remote.</summary>
  131. internal string ui2_remote;
  132.  
  133. /// <summary>The ui 2_password.</summary>
  134. internal string ui2_password;
  135.  
  136. /// <summary>The ui 2_status.</summary>
  137. internal uint ui2_status;
  138.  
  139. /// <summary>The ui 2_asg_type.</summary>
  140. internal uint ui2_asg_type;
  141.  
  142. /// <summary>The ui 2_refcount.</summary>
  143. internal uint ui2_refcount;
  144.  
  145. /// <summary>The ui 2_usecount.</summary>
  146. internal uint ui2_usecount;
  147.  
  148. /// <summary>The ui 2_username.</summary>
  149. internal string ui2_username;
  150.  
  151. /// <summary>The ui 2_domainname.</summary>
  152. internal string ui2_domainname;
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement