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# 共享文件读取(转)的更多相关文章

  1. linux共享文件samba安装与java读取外部文件夹方法

    测试环境RedHat 6.4 一.安装 samba组件安装: (1)首先用“rpm –qa |grep samba”命令检验系统samba服务是否安装. #rpm –qa |grep samba sa ...

  2. SQL 横转竖 、竖专横 (转载) 使用Dapper.Contrib 开发.net core程序,兼容多种数据库 C# 读取PDF多级书签 Json.net日期格式化设置 ASPNET 下载共享文件 ASPNET 文件批量下载 递归,循环,尾递归 利用IDisposable接口构建包含非托管资源对象 《.NET 进阶指南》读书笔记2------定义不可改变类型

    SQL 横转竖 .竖专横 (转载)   普通行列转换 问题:假设有张学生成绩表(tb)如下: 姓名 课程 分数 张三 语文 74 张三 数学 83 张三 物理 93 李四 语文 74 李四 数学 84 ...

  3. jcifs 具体解释读取网络共享文件数据

    时隔1年半,没有发过新的帖子了,也没怎么来过CSDN逛逛了,人也懒散了. 今天收到网友的提问,才回来看看.认为应该再写点什么出来.只是.发现自己研究是不是太深入,写不出那么高深的东西.那就写点肤浅的东 ...

  4. windows共享文件夹如何让CentOS 6.5读取

    http://www.111cn.net/sys/CentOS/74104.htm 工作需要,需要把本地win7共享的文件夹让CenotOS 6.5服务器临时使用一下,以下是CentOS 6.5系统挂 ...

  5. PHP 读取文件夹(比如某共享文件夹)中的图片并显示

    1.获取文件夹下图片public function albumList(){ $share_url = input('path'); $files = getImgList($share_url); ...

  6. IIS访问共享文件详解

    前言 公司同事做了一个报表系统,需要做集群部署,本来是一件挺容易的事,但是部署过程中却遇到啦种种蛋疼问题. 问题1.我们的报表使用的是微软的水晶报表,需要上传报表的配置文件,然后水晶报表提供的控件来读 ...

  7. [虚拟机]Virtual Box的使用--共享文件夹

    最近为系统测试使用了虚拟机,VM太卡,所以使用了VBox,运行效果还不错 为了主机和客户机之前方便进行数据传输,一般采用文件夹共享的方式(当然,可以直接拖拽) 1,直接拖拽,需要做如下设置 主要的是“ ...

  8. Win10/UWP新特性—SharedStorageAccessManager 共享文件

    首先先给大家推荐一个UWP/Win10开发者群:53078485  里面有很多大婶,还有很多学习资源,欢迎大家来一起讨论Win10开发! 在UWP开发中,微软提供了一个新的特性叫做SharedStor ...

  9. c#下载共享文件夹下的文件并记录错误日志

    public void Run() { //获取目标文件列表 string _ErrorMessage = ""; string _ErrorMessageFile = " ...

随机推荐

  1. CSS中的自适应单位vw、vh、vmin、vmax

    1.vw.vh.vmin.vmax各单位的意义 上面的自适应单位可以统称为视口单位. 可以先了解一下视口指的是什么? 在PC端,视口指的是在PC端,指的是浏览器的可视区域:而在移动端,它涉及3个视口: ...

  2. Java——动态绑定和多态

    动态绑定是在执行期间(而不是编译期间)判断所用对象的实际类型,根据其实际的类型调用其相应的方法(确定选择哪个多态形式被调用). 首先是方法的参数是父类对象,传入子类对象是否可行 然后引出Parent ...

  3. codevs 2039 骑马修栅栏 USACO x

    题目描述 Description Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏 ...

  4. [JSOI2008]最大数 题解

    前言 巨佬说:要有线段树,于是蒟蒻就开始打线段树. 蒟蒻只能拿之前0分的板子题开刀了QAQ. 题解 一开始我以为插入操作不带取模,于是打了下面这个弱智玩意 下面的代码是会WA的 #include &l ...

  5. Spring Cloud教程(十二)加密和解密

    Spring Cloud具有一个用于在本地解密属性值的Environment预处理器.它遵循与Config Server相同的规则,并通过encrypt.*具有相同的外部配置.因此,您可以使用{cip ...

  6. 版本基线自动化之Linux

    上一篇笔者叙述了如何在windows平台上进行自动化打包,这次采用linux平台 1.start.bat : 调用cleanall.bat脚本 ,从svn服务器中检出代码,并采用7-Zip工具进行压缩 ...

  7. [CSP-S模拟测试]:f(Trie树+二分答案+meet in middle+two pointers)

    题目传送门(内部题67) 输入格式 第一行,三个整数$n$.$k$.$p$.第二行,$n$个自然数,表示$\{a_i\}$. 输出格式 输出一行,两个自然数,表示$f(res)$.$res$. 样例 ...

  8. qbzt day3 晚上 平衡树的一些思想

    pks大佬的blog 二叉查找树 任何一个节点左子树的所有元素都小于这个节点,右子树的所有元素都大于这个节点 查找一个节点:从根节点开始,比他小就向左走,比他大就向右走 平衡树:解决二叉查找树的一些痛 ...

  9. jenkins执行 pod install 报错 CocoaPods requires your terminal to be using UTF-8 encoding. Consider adding the following to ~/.profile:

    错误提示是: CocoaPods 需要终端使用utf-8编码 解决办法

  10. error: exportArchive: You don’t have permission to save the file “HelloWorld.ipa” in the folder “HelloWorld”.

    成功clean环境和生成archive文件之后,最后一步导出ipa包,遇到了权限问题: you don’t have permission to save the file “HelloWorld.i ...