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();
}

}
}

最新文章

  1. AngularJS四大特性
  2. HTTP协议学习---(七)代理
  3. NOSQL Benchmarks
  4. 大端小端(Big- Endian和Little-Endian)[转]
  5. Swift - 38 - 枚举的基本语法
  6. 存储过程中update,然后用sql%判断update是否成功的存储过程
  7. SQL Server 2008 2005删除或压缩数据库日志的方法
  8. 处理SFTP服务器上已离职用户,设置为登录禁用状态
  9. angularjs的几种常见写法
  10. Windows Sublime Text 配置Linux子系统(WSL)下的 gcc/g++ 编译环境
  11. CASE WHEN用法
  12. 微服务浪潮中,程序猿如何让自己 Be Cloud Native
  13. python中logger模块的应用
  14. 小白眼中的AI之~Numpy基础
  15. django-admin 仿写stark组件action,filter筛选过滤,search查询
  16. 『PyTorch』第五弹_深入理解Tensor对象_下:从内存看Tensor
  17. Innodb引擎简介
  18. Python学习笔记第九周
  19. Latex 表格(跨行、跨列、背景加灰)new
  20. Mockplus是如何节省你的原型时间的?

热门文章

  1. 【leetcode】1103. Distribute Candies to People
  2. LeetCode 1143 最长公共子序列
  3. android 8.0 适配(总结)
  4. PHP培训教程 PHP的运算符
  5. poj 3613Cow Relays
  6. CSS——小三角带边框带阴影
  7. 170929-关于md5加密
  8. 使用java操作elasticsearch之与es简历连接
  9. WCF - Hosting WCF Service 四种托管方式
  10. 解决Firefox已阻止运行早期版本Adobe Flash