Using System;
Using System.Runtime.InteropServices;
Using BOOL = System.Boolean;
Using DWORD = System.UInt32;
Using LPWSTR = System.String;
Using Net_api_status = System.UInt32;
Namespace Connectuncwithcredentials
{
public class Uncaccesswithcredentials:idisposable
{
[StructLayout (layoutkind.sequential, CharSet = CharSet.Unicode)]
internal struct use_info_2
{
Internal LPWSTR ui2_local;
Internal LPWSTR ui2_remote;
Internal LPWSTR Ui2_password;
Internal DWORD ui2_status;
Internal DWORD Ui2_asg_type;
Internal DWORD Ui2_refcount;
Internal DWORD Ui2_usecount;
Internal LPWSTR ui2_username;
Internal LPWSTR ui2_domainname;
}
[DllImport ("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
Internal static extern Net_api_status NetUseAdd (
LPWStr Uncservername,
DWORD level,
Ref Use_info_2 Buf,
Out DWORD Parmerror);
[DllImport ("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
Internal static extern Net_api_status Netusedel (
LPWStr Uncservername,
LPWStr Usename,
DWORD Forcecond);
private bool disposed = false;
private string Suncpath;
private string Suser;
private string Spassword;
private string Sdomain;
private int ilasterror;
<summary>
A disposeable class that allows access to a UNC resource with credentials.
</summary>
Public Uncaccesswithcredentials ()
{
}
<summary>
The last system error code returned from NetUseAdd or Netusedel. Success = 0
</summary>
public int LastError
{
get {return ilasterror;}
}
public void Dispose ()
{
if (!this.disposed)
{
Netusedelete ();
}
disposed = true;
Gc. SuppressFinalize (this);
}
<summary>
Connects to a UNC path using the credentials supplied.
</summary>
<param name= "Uncpath" >fully qualified domain name UNC path</param>
<param name= "user" >a user with sufficient rights to access the Path.</param>
<param name= "Domain" >domain of user.</param>
<param name= "Password" >password of user</param>
<returns>true if mapping succeeds. Use LastError to get the system error code.</returns>
public bool Netusewithcredentials (string Uncpath, String User, String Domain, String Password)
{
Suncpath = Uncpath;
Suser = User;
Spassword = Password;
Sdomain = Domain;
return Netusewithcredentials ();
}
private bool Netusewithcredentials ()
{
UINT ReturnCode;
Try
{
Use_info_2 useinfo = new Use_info_2 ();
Useinfo.ui2_remote = Suncpath;
Useinfo.ui2_username = Suser;
Useinfo.ui2_domainname = Sdomain;
Useinfo.ui2_password = Spassword;
Useinfo.ui2_asg_type = 0;
Useinfo.ui2_usecount = 1;
UINT Paramerrorindex;
ReturnCode = NetUseAdd (null, 2, ref useinfo, out Paramerrorindex);
Ilasterror = (int) ReturnCode;
return ReturnCode = = 0;
}
Catch
{
Ilasterror = Marshal.GetLastWin32Error ();
return false;
}
}
<summary>
Ends the connection to the remote resource
</summary>
<returns>true if it succeeds. Use LastError to get the system error code</returns>
public bool Netusedelete ()
{
UINT ReturnCode;
Try
{
ReturnCode = Netusedel (null, Suncpath, 2);
Ilasterror = (int) ReturnCode;
return (ReturnCode = = 0);
}
Catch
{
Ilasterror = Marshal.GetLastWin32Error ();
return false;
}
}
~uncaccesswithcredentials ()
{
Dispose ();
}
}
}
C # shared file read (GO)