NetworkShareAccesser: 远程PC1 文件 copy 到PC2 文件夹
Usage:
string strRepoBundlePath = @"\\at1-repo01\ATE\Bundles\SharePoint\Open\denyopen.zip";
string strRemoteBundle = @"\\"+serverHostName+@"\c$\denyopen.zip"; using (NetworkShareAccesser.Access(serverHostName, domain, userName, password))
{
File.Copy(@strRepoBundlePath, @strRemoteBundle);
}
WARNING: Please make absolutely sure, that Dispose
of the NetworkShareAccesser
is called (even if you app crashes!), otherwise an open connection will remain on Windows. You can see all open connections by opening the cmd
prompt and enter net use
.
The Code:
public class NetworkShareAccesser : IDisposable
{
private string _remoteUncName;
private string _remoteComputerName; public string RemoteComputerName
{
get
{
return this._remoteComputerName;
}
set
{
this._remoteComputerName = value;
this._remoteUncName = @"\\" + this._remoteComputerName;
}
} public string UserName
{
get;
set;
}
public string Password
{
get;
set;
} #region Consts private const int RESOURCE_CONNECTED = 0x00000001;
private const int RESOURCE_GLOBALNET = 0x00000002;
private const int RESOURCE_REMEMBERED = 0x00000003; private const int RESOURCETYPE_ANY = 0x00000000;
private const int RESOURCETYPE_DISK = 0x00000001;
private const int RESOURCETYPE_PRINT = 0x00000002; private const int RESOURCEDISPLAYTYPE_GENERIC = 0x00000000;
private const int RESOURCEDISPLAYTYPE_DOMAIN = 0x00000001;
private const int RESOURCEDISPLAYTYPE_SERVER = 0x00000002;
private const int RESOURCEDISPLAYTYPE_SHARE = 0x00000003;
private const int RESOURCEDISPLAYTYPE_FILE = 0x00000004;
private const int RESOURCEDISPLAYTYPE_GROUP = 0x00000005; private const int RESOURCEUSAGE_CONNECTABLE = 0x00000001;
private const int RESOURCEUSAGE_CONTAINER = 0x00000002; private const int CONNECT_INTERACTIVE = 0x00000008;
private const int CONNECT_PROMPT = 0x00000010;
private const int CONNECT_REDIRECT = 0x00000080;
private const int CONNECT_UPDATE_PROFILE = 0x00000001;
private const int CONNECT_COMMANDLINE = 0x00000800;
private const int CONNECT_CMD_SAVECRED = 0x00001000; private const int CONNECT_LOCALDRIVE = 0x00000100; #endregion #region Errors private const int NO_ERROR = ; private const int ERROR_ACCESS_DENIED = ;
private const int ERROR_ALREADY_ASSIGNED = ;
private const int ERROR_BAD_DEVICE = ;
private const int ERROR_BAD_NET_NAME = ;
private const int ERROR_BAD_PROVIDER = ;
private const int ERROR_CANCELLED = ;
private const int ERROR_EXTENDED_ERROR = ;
private const int ERROR_INVALID_ADDRESS = ;
private const int ERROR_INVALID_PARAMETER = ;
private const int ERROR_INVALID_PASSWORD = ;
private const int ERROR_MORE_DATA = ;
private const int ERROR_NO_MORE_ITEMS = ;
private const int ERROR_NO_NET_OR_BAD_PATH = ;
private const int ERROR_NO_NETWORK = ; private const int ERROR_BAD_PROFILE = ;
private const int ERROR_CANNOT_OPEN_PROFILE = ;
private const int ERROR_DEVICE_IN_USE = ;
private const int ERROR_NOT_CONNECTED = ;
private const int ERROR_OPEN_FILES = ; #endregion #region PInvoke Signatures [DllImport("Mpr.dll")]
private static extern int WNetUseConnection(
IntPtr hwndOwner,
NETRESOURCE lpNetResource,
string lpPassword,
string lpUserID,
int dwFlags,
string lpAccessName,
string lpBufferSize,
string lpResult
); [DllImport("Mpr.dll")]
private static extern int WNetCancelConnection2(
string lpName,
int dwFlags,
bool fForce
); [StructLayout(LayoutKind.Sequential)]
private class NETRESOURCE
{
public int dwScope = ;
public int dwType = ;
public int dwDisplayType = ;
public int dwUsage = ;
public string lpLocalName = "";
public string lpRemoteName = "";
public string lpComment = "";
public string lpProvider = "";
} #endregion /// <summary>
/// Creates a NetworkShareAccesser for the given computer name. The user will be promted to enter credentials
/// </summary>
/// <param name="remoteComputerName"></param>
/// <returns></returns>
public static NetworkShareAccesser Access(string remoteComputerName)
{
return new NetworkShareAccesser(remoteComputerName);
} /// <summary>
/// Creates a NetworkShareAccesser for the given computer name using the given domain/computer name, username and password
/// </summary>
/// <param name="remoteComputerName"></param>
/// <param name="domainOrComuterName"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
public static NetworkShareAccesser Access(string remoteComputerName, string domainOrComuterName, string userName, string password)
{
return new NetworkShareAccesser(remoteComputerName,
domainOrComuterName + @"\" + userName,
password);
} /// <summary>
/// Creates a NetworkShareAccesser for the given computer name using the given username (format: domainOrComputername\Username) and password
/// </summary>
/// <param name="remoteComputerName"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
public static NetworkShareAccesser Access(string remoteComputerName, string userName, string password)
{
return new NetworkShareAccesser(remoteComputerName,
userName,
password);
} private NetworkShareAccesser(string remoteComputerName)
{
RemoteComputerName = remoteComputerName; this.ConnectToShare(this._remoteUncName, null, null, true);
} private NetworkShareAccesser(string remoteComputerName, string userName, string password)
{
RemoteComputerName = remoteComputerName;
UserName = userName;
Password = password; this.ConnectToShare(this._remoteUncName, this.UserName, this.Password, false);
} private void ConnectToShare(string remoteUnc, string username, string password, bool promptUser)
{
NETRESOURCE nr = new NETRESOURCE
{
dwType = RESOURCETYPE_DISK,
lpRemoteName = remoteUnc
}; int result;
if (promptUser)
{
result = WNetUseConnection(IntPtr.Zero, nr, "", "", CONNECT_INTERACTIVE | CONNECT_PROMPT, null, null, null);
}
else
{
result = WNetUseConnection(IntPtr.Zero, nr, password, username, , null, null, null);
} if (result != NO_ERROR)
{
throw new Win32Exception(result);
}
} private void DisconnectFromShare(string remoteUnc)
{
int result = WNetCancelConnection2(remoteUnc, CONNECT_UPDATE_PROFILE, false);
if (result != NO_ERROR)
{
throw new Win32Exception(result);
}
} /// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority></filterpriority>
public void Dispose()
{
this.DisconnectFromShare(this._remoteUncName);
}
}
NetworkShareAccesser: 远程PC1 文件 copy 到PC2 文件夹的更多相关文章
- 02-大文件Copy(FileStream文件流类)
static void Main(string[] args) { string source = @"e:\1.exe";//要移动文件的路径 大文件 string target ...
- SCP“免密” 远程COPY较多文件
一.linux脚本实现自动输入密码 使用Linux的程序员对输入密码这个举动一定不陌生,在Linux下对用户有严格的权限限制,干很多事情越过了权限就得输入密码,比如使用超级用户执行命令,又比如ftp. ...
- 【Linux】scp“免密” 远程copy较多文件
一.linux脚本实现自动输入密码 使用Linux的程序员对输入密码这个举动一定不陌生,在Linux下对用户有严格的权限限制,干很多事情越过了权限就得输入密码,比如使用超级用户执行命令,又比如ftp. ...
- hadoop搭建杂记:Linux下不同linux主机之间文件copy的scp命令
不同的Linux之间copy文件常用有3种方法: 不同的Linux之间copy文件常用有3种方法: ①ftp 就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的程序来进行文件 ...
- 钓鱼攻击之远程加载恶意Word模版文件上线CS
0x00 前言 利用Word文档加载附加模板时的缺陷所发起的恶意请求而达到的攻击目的,所以当目标用户点开攻击者发给他的恶意word文档就可以通过向远程服务器请求恶意模板并执行恶意模板上的恶意代码.这里 ...
- 自定义从Azure下载回来的远程桌面连接(.rdp)文件,使其提供更多丰富功能
通常情况下,我们使用Azure的时候微软会为用户提供可连接管理虚拟机的远程桌面服务,并且支持文件实例的保存,对于Windows 系统来说,微软提供的链接文件实例就是.rdp文件. 获得.rdp文件 打 ...
- C# FTP远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)
今天用代码删除FTP服务器上的目录时候,报错:远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件). 习惯性的google,不外乎以下几点: 1.URL路径不对,看看有没有多加 ...
- 文件Copy和文件夹Copy
文件Copy和文件夹Copy using System.Collections.Generic; using System.Linq; using System.Text; using System. ...
- php不使用copy()函数复制文件的方法
本文实例讲述了php不使用copy()函数复制文件的方法.分享给大家供大家参考.具体如下:下面的代码不使用php内置的copy函数,直接通过文件读取写入的操作方式复制文件 <?php funct ...
随机推荐
- Nodejs发送Post请求时出现socket hang up错误的解决办法
参考nodejs官网发送http post请求的方法,实现了一个模拟post提交的功能.实际使用时报socket hang up错误. 后来发现是请求头设置的问题,发送选项中需要加上headers字段 ...
- Populating Display Item Value On Query In Oracle Forms
Write Post-Query trigger for the block you want to fetch the field value for display item.ExampleBeg ...
- centos 扩展root根分区的大小
目标:将VolGroup-lv_home缩小到125G,并将剩余的空间添加给VolGroup-lv_root 1.首先查看磁盘使用情况[root@localhost ~]# df -h文件系统 ...
- linux Chrome 安装
1.wget 32bits: wget https://dl.google.com/linux/direct/google-chrome-stable_current_i386.deb 64bits: ...
- 单向和双向tvs管
tvs管器件按极性可分为单极性和双极性两种,即单向tvs管和双向tvs管. 单向tvs管保护器件仅能对正脉冲或者负脉冲进行防护,而双向tvs管保护器件一端接要保护的线路,一端接地,无论来自反向还 ...
- OGNL调用静态方法和属性
ognl的全名是 Object-Graph Navigation Language 表示的是图对象导航语言...我觉得它最厉害的一点是,通过"."来实现对象的导航...下面看他他的 ...
- Socket&GCDAsyncSocket(异步Socket)
Socket ********************************************* 简单理解Socket 就是网络连接,可以实现两个点之间的数据通讯. •Socket允许使用长连 ...
- 移动端 移动web屏幕适配方案 随不同宽度的屏幕而改变
链接地址1:http://www.cnblogs.com/zjzhome/p/4802157.html 链接地址2:http://www.html-js.com/article/Mobile-term ...
- Java初始化(成员变量)
java尽力保证:所有变量在使用前都能得到恰当的初始化.对于方法的局部变量,java以编译时错误的形式来贯彻这种保证.如下面代码: public class TestJava { void test( ...
- w3c与微软(IE)事件注册区别 -Tom
严格来说,有2中不同的模型:W3C模型和微软模型,除IE之外W3C模型支持所有的现代浏览器,而微软模型只支持IE,使用W3C模型的代码如下: // 格式:target.addEventListener ...