C# 共享文件读取(转)
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# 共享文件读取(转)的更多相关文章
- linux共享文件samba安装与java读取外部文件夹方法
测试环境RedHat 6.4 一.安装 samba组件安装: (1)首先用“rpm –qa |grep samba”命令检验系统samba服务是否安装. #rpm –qa |grep samba sa ...
- SQL 横转竖 、竖专横 (转载) 使用Dapper.Contrib 开发.net core程序,兼容多种数据库 C# 读取PDF多级书签 Json.net日期格式化设置 ASPNET 下载共享文件 ASPNET 文件批量下载 递归,循环,尾递归 利用IDisposable接口构建包含非托管资源对象 《.NET 进阶指南》读书笔记2------定义不可改变类型
SQL 横转竖 .竖专横 (转载) 普通行列转换 问题:假设有张学生成绩表(tb)如下: 姓名 课程 分数 张三 语文 74 张三 数学 83 张三 物理 93 李四 语文 74 李四 数学 84 ...
- jcifs 具体解释读取网络共享文件数据
时隔1年半,没有发过新的帖子了,也没怎么来过CSDN逛逛了,人也懒散了. 今天收到网友的提问,才回来看看.认为应该再写点什么出来.只是.发现自己研究是不是太深入,写不出那么高深的东西.那就写点肤浅的东 ...
- windows共享文件夹如何让CentOS 6.5读取
http://www.111cn.net/sys/CentOS/74104.htm 工作需要,需要把本地win7共享的文件夹让CenotOS 6.5服务器临时使用一下,以下是CentOS 6.5系统挂 ...
- PHP 读取文件夹(比如某共享文件夹)中的图片并显示
1.获取文件夹下图片public function albumList(){ $share_url = input('path'); $files = getImgList($share_url); ...
- IIS访问共享文件详解
前言 公司同事做了一个报表系统,需要做集群部署,本来是一件挺容易的事,但是部署过程中却遇到啦种种蛋疼问题. 问题1.我们的报表使用的是微软的水晶报表,需要上传报表的配置文件,然后水晶报表提供的控件来读 ...
- [虚拟机]Virtual Box的使用--共享文件夹
最近为系统测试使用了虚拟机,VM太卡,所以使用了VBox,运行效果还不错 为了主机和客户机之前方便进行数据传输,一般采用文件夹共享的方式(当然,可以直接拖拽) 1,直接拖拽,需要做如下设置 主要的是“ ...
- Win10/UWP新特性—SharedStorageAccessManager 共享文件
首先先给大家推荐一个UWP/Win10开发者群:53078485 里面有很多大婶,还有很多学习资源,欢迎大家来一起讨论Win10开发! 在UWP开发中,微软提供了一个新的特性叫做SharedStor ...
- c#下载共享文件夹下的文件并记录错误日志
public void Run() { //获取目标文件列表 string _ErrorMessage = ""; string _ErrorMessageFile = " ...
随机推荐
- 打包组件assembly之package.xml
形如: <assembly> <id>deploy</id> <formats> <format>zip</format> &l ...
- jquery 父,子,兄弟节点获取
jquery 父,子,兄弟节点获取 jQuery.parent(expr) //找父元素 jQuery.parents(expr) //找到所有祖先元素,不限于父 ...
- JSP页面与html页面在ie下显示的样式不一致的问题
今天前端将样式与html给我文件我转化为jsp之后在我的电脑上使用IE11的IE9和其他浏览器都没有问题,但是在发给其他人检查的时候却发现在win7电脑的IE9上出现样式错乱的问题,前端调试无果的情况 ...
- Redis-cli相关命令
> flushdb > SELECT OK []> GET db_number (nil) []> []> flushdb OK []> SELECT OK > ...
- IOC和AOP使用扩展之AOP详解实现类
摘要: “Depend on yourself” is what nature says to every man. Parents can help you. Teachers can hel ...
- Internet History, Technology, and Security(week5)——Technology: Internets and Packets
前言: 之前都在学习Internet的历史,从这周开始,进入到了Internet技术的学习. Layer1: Link Introduction / The Link Layer 80年代之前,主流网 ...
- [CSP-S模拟测试]:辣鸡(ljh) (暴力)
题目描述 辣鸡$ljh\ NOI$之后就退役了,然后就滚去学文化课了.然而在上化学课的时候,数学和化学都不好的$ljh$却被一道简单题难住了,受到了大佬的嘲笑.题目描述是这样的:在一个二维平面上有一层 ...
- 转载自:StringUtils的常见方法
转载自:https://blog.csdn.net/simple_smile_sun/article/details/51819158 注:运用StringUtils需要导入相关jar文件,commo ...
- Xcode工程文件pbxproj
Xcode工程文件pbxproj Xcode会去读Project.pbxproj文件,把pbxproj转成plist文件,看起根目录结构 rootObject:指向的是我们的工程对象.(对应一个24个 ...
- url传输中+转为%2B取到变空格的问题
一个加密串,通过url传过来后怎么也解密不了. 原串: h1tQYMmnx7jdvowi9KnaEM5dHfamJ+rbaRnvhwhUmRAvyxpA2AGzszx5k9K0qdhiQUq7m42S ...