NetworkShareAccesser: 远程PC1 文件 copy 到PC2 文件夹
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了NetworkShareAccesser: 远程PC1 文件 copy 到PC2 文件夹,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6490字,纯文字阅读大概需要10分钟。
内容图文
![NetworkShareAccesser: 远程PC1 文件 copy 到PC2 文件夹](/upload/InfoBanner/zyjiaocheng/1045/02e69db5ee6a4345bab6b7a181c2a830.jpg)
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; } } publicstring UserName { get; set; } publicstring Password { get; set; } #region Consts privateconstint RESOURCE_CONNECTED = 0x00000001; privateconstint RESOURCE_GLOBALNET = 0x00000002; privateconstint RESOURCE_REMEMBERED = 0x00000003; privateconstint RESOURCETYPE_ANY = 0x00000000; privateconstint RESOURCETYPE_DISK = 0x00000001; privateconstint RESOURCETYPE_PRINT = 0x00000002; privateconstint RESOURCEDISPLAYTYPE_GENERIC = 0x00000000; privateconstint RESOURCEDISPLAYTYPE_DOMAIN = 0x00000001; privateconstint RESOURCEDISPLAYTYPE_SERVER = 0x00000002; privateconstint RESOURCEDISPLAYTYPE_SHARE = 0x00000003; privateconstint RESOURCEDISPLAYTYPE_FILE = 0x00000004; privateconstint RESOURCEDISPLAYTYPE_GROUP = 0x00000005; privateconstint RESOURCEUSAGE_CONNECTABLE = 0x00000001; privateconstint RESOURCEUSAGE_CONTAINER = 0x00000002; privateconstint CONNECT_INTERACTIVE = 0x00000008; privateconstint CONNECT_PROMPT = 0x00000010; privateconstint CONNECT_REDIRECT = 0x00000080; privateconstint CONNECT_UPDATE_PROFILE = 0x00000001; privateconstint CONNECT_COMMANDLINE = 0x00000800; privateconstint CONNECT_CMD_SAVECRED = 0x00001000; privateconstint CONNECT_LOCALDRIVE = 0x00000100; #endregion#region Errors privateconstint NO_ERROR = 0; privateconstint ERROR_ACCESS_DENIED = 5; privateconstint ERROR_ALREADY_ASSIGNED = 85; privateconstint ERROR_BAD_DEVICE = 1200; privateconstint ERROR_BAD_NET_NAME = 67; privateconstint ERROR_BAD_PROVIDER = 1204; privateconstint ERROR_CANCELLED = 1223; privateconstint ERROR_EXTENDED_ERROR = 1208; privateconstint ERROR_INVALID_ADDRESS = 487; privateconstint ERROR_INVALID_PARAMETER = 87; privateconstint ERROR_INVALID_PASSWORD = 1216; privateconstint ERROR_MORE_DATA = 234; privateconstint ERROR_NO_MORE_ITEMS = 259; privateconstint ERROR_NO_NET_OR_BAD_PATH = 1203; privateconstint ERROR_NO_NETWORK = 1222; privateconstint ERROR_BAD_PROFILE = 1206; privateconstint ERROR_CANNOT_OPEN_PROFILE = 1205; privateconstint ERROR_DEVICE_IN_USE = 2404; privateconstint ERROR_NOT_CONNECTED = 2250; privateconstint ERROR_OPEN_FILES = 2401; #endregion#region PInvoke Signatures [DllImport("Mpr.dll")] privatestaticexternint WNetUseConnection( IntPtr hwndOwner, NETRESOURCE lpNetResource, string lpPassword, string lpUserID, int dwFlags, string lpAccessName, string lpBufferSize, string lpResult ); [DllImport("Mpr.dll")] privatestaticexternint WNetCancelConnection2( string lpName, int dwFlags, bool fForce ); [StructLayout(LayoutKind.Sequential)] privateclass NETRESOURCE { publicint dwScope = 0; publicint dwType = 0; publicint dwDisplayType = 0; publicint dwUsage = 0; publicstring lpLocalName = ""; publicstring lpRemoteName = ""; publicstring lpComment = ""; publicstring 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>publicstatic NetworkShareAccesser Access(string remoteComputerName) { returnnew 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>publicstatic NetworkShareAccesser Access(string remoteComputerName, string domainOrComuterName, string userName, string password) { returnnew 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>publicstatic NetworkShareAccesser Access(string remoteComputerName, string userName, string password) { returnnew 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); } privatevoid 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, 0, null, null, null); } if (result != NO_ERROR) { thrownew Win32Exception(result); } } privatevoid DisconnectFromShare(string remoteUnc) { int result = WNetCancelConnection2(remoteUnc, CONNECT_UPDATE_PROFILE, false); if (result != NO_ERROR) { thrownew Win32Exception(result); } } ///<summary>/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. ///</summary>///<filterpriority>2</filterpriority>publicvoid Dispose() { this.DisconnectFromShare(this._remoteUncName); } }
原文:http://www.cnblogs.com/baby0814/p/4383732.html
内容总结
以上是互联网集市为您收集整理的NetworkShareAccesser: 远程PC1 文件 copy 到PC2 文件夹全部内容,希望文章能够帮你解决NetworkShareAccesser: 远程PC1 文件 copy 到PC2 文件夹所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。