Skip to content

Instantly share code, notes, and snippets.

@danbarua
Created January 18, 2017 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danbarua/0697a77dc0fb1d74e37135c08decdab9 to your computer and use it in GitHub Desktop.
Save danbarua/0697a77dc0fb1d74e37135c08decdab9 to your computer and use it in GitHub Desktop.
Access
/// <summary>
/// Helper methods for mounting a UNC network share
/// </summary>
public class UncAccess
{
/// <summary>The domain.</summary>
private string domain;
/// <summary>The last error.</summary>
private int lastError;
/// <summary>The password.</summary>
private string password;
/// <summary>The unc path.</summary>
private string uncPath;
/// <summary>The user.</summary>
private string user;
/// <summary>Initialises a new instance of the <see cref="UncAccess"/> class.</summary>
public UncAccess()
{
}
/// <summary>Initialises a new instance of the <see cref="UncAccess"/> class.</summary>
/// <param name="uncPath">The unc path.</param>
/// <param name="user">The user.</param>
/// <param name="domain">The domain.</param>
/// <param name="password">The password.</param>
public UncAccess(string uncPath, string user, string domain, string password)
{
this.Login(uncPath, user, domain, password);
}
/// <summary>Gets the last error.</summary>
public int LastError
{
get
{
return this.lastError;
}
}
/// <summary>
/// Closes the UNC share
/// </summary>
/// <returns>True if closing was successful</returns>
public bool NetUseDelete()
{
try
{
var returncode = NetUseDel(null, this.uncPath, 2);
this.lastError = (int)returncode;
return returncode == 0;
}
catch
{
this.lastError = Marshal.GetLastWin32Error();
return false;
}
}
/// <summary>Connects to a UNC share folder with credentials</summary>
/// <param name="uncPath">The unc Path.</param>
/// <param name="user">The user.</param>
/// <param name="domain">The domain.</param>
/// <param name="password">The password.</param>
/// <returns>True if login was successful</returns>
public bool Login(string uncPath, string user, string domain, string password)
{
this.uncPath = uncPath;
this.user = user;
this.password = password;
this.domain = domain;
return this.NetUseWithCredentials();
}
/// <summary>The net use add.</summary>
/// <param name="uncServerName">The unc server name.</param>
/// <param name="level">The level.</param>
/// <param name="buf">The buf.</param>
/// <param name="parmError">The parm error.</param>
/// <returns>The <see cref="uint"/>.</returns>
[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern uint NetUseAdd(string uncServerName, uint level, ref UseInfo2 buf, out uint parmError);
/// <summary>The net use del.</summary>
/// <param name="uncServerName">The unc server name.</param>
/// <param name="useName">The use name.</param>
/// <param name="forceCond">The force cond.</param>
/// <returns>The <see cref="uint"/>.</returns>
[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern uint NetUseDel(string uncServerName, string useName, uint forceCond);
/// <summary>The net use with credentials.</summary>
/// <returns>The <see cref="bool"/>.</returns>
private bool NetUseWithCredentials()
{
try
{
var useinfo = new UseInfo2
{
ui2_remote = this.uncPath, ui2_username = this.user, ui2_domainname = this.domain, ui2_password = this.password,
ui2_asg_type = 0, ui2_usecount = 1
};
uint paramErrorIndex;
var returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
this.lastError = (int)returncode;
return returncode == 0;
}
catch
{
this.lastError = Marshal.GetLastWin32Error();
return false;
}
}
/// <summary>The use info 2.</summary>
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
Justification = "Reviewed. Suppression is OK here.")]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct UseInfo2
{
/// <summary>The ui 2_local.</summary>
internal string ui2_local;
/// <summary>The ui 2_remote.</summary>
internal string ui2_remote;
/// <summary>The ui 2_password.</summary>
internal string ui2_password;
/// <summary>The ui 2_status.</summary>
internal uint ui2_status;
/// <summary>The ui 2_asg_type.</summary>
internal uint ui2_asg_type;
/// <summary>The ui 2_refcount.</summary>
internal uint ui2_refcount;
/// <summary>The ui 2_usecount.</summary>
internal uint ui2_usecount;
/// <summary>The ui 2_username.</summary>
internal string ui2_username;
/// <summary>The ui 2_domainname.</summary>
internal string ui2_domainname;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment