C # shared file read

Source: Internet
Author: User
Tags fully qualified domain name

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.