using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
using System.Windows.Forms;
namespace LogOperator.Class
{
#region lpNetResouce结构体
public enum ERROR_ID
{
ERROR_SUCCESS = 0, // Success
ERROR_BUSY = 170,
ERROR_MORE_DATA = 234,
ERROR_NO_BROWSER_SERVERS_FOUND = 6118,
ERROR_INVALID_LEVEL = 124,
ERROR_ACCESS_DENIED = 5,
ERROR_INVALID_PASSWORD = 86,
ERROR_INVALID_PARAMETER = 87,
ERROR_BAD_DEV_TYPE = 66,
ERROR_NOT_ENOUGH_MEMORY = 8,
ERROR_NETWORK_BUSY = 54,
ERROR_BAD_NETPATH = 53,
ERROR_NO_NETWORK = 1222,
ERROR_INVALID_HANDLE_STATE = 1609,
ERROR_EXTENDED_ERROR = 1208,
ERROR_DEVICE_ALREADY_REMEMBERED = 1202,
ERROR_NO_NET_OR_BAD_PATH = 1203
}
public enum RESOURCE_SCOPE
{
RESOURCE_CONNECTED = 1, //枚举连接的资源
RESOURCE_GLOBALNET = 2, //枚举所有资源
RESOURCE_REMEMBERED = 3,//枚举永久性连接
RESOURCE_RECENT = 4,
RESOURCE_CONTEXT = 5
}
public enum RESOURCE_TYPE
{
RESOURCETYPE_ANY = 0,//枚举所有资源
RESOURCETYPE_DISK = 1,//枚举磁盘
RESOURCETYPE_PRINT = 2,//枚举打印机
RESOURCETYPE_RESERVED = 8,
}
public enum RESOURCE_USAGE
{
RESOURCEUSAGE_CONNECTABLE = 1,//可同这个资源连接
RESOURCEUSAGE_CONTAINER = 2,//这个资源包含了可以枚举的额外资源
RESOURCEUSAGE_NOLOCALDEVICE = 4,
RESOURCEUSAGE_SIBLING = 8,
RESOURCEUSAGE_ATTACHED = 16,
RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
}
public enum RESOURCE_DISPLAYTYPE
{
RESOURCEDISPLAYTYPE_GENERIC = 0,
RESOURCEDISPLAYTYPE_DOMAIN = 1,
RESOURCEDISPLAYTYPE_SERVER = 2,
RESOURCEDISPLAYTYPE_SHARE = 3,
RESOURCEDISPLAYTYPE_FILE = 4,
RESOURCEDISPLAYTYPE_GROUP = 5,
RESOURCEDISPLAYTYPE_NETWORK = 6,
RESOURCEDISPLAYTYPE_ROOT = 7,
RESOURCEDISPLAYTYPE_SHAREADMIN = 8,
RESOURCEDISPLAYTYPE_DIRECTORY = 9,
RESOURCEDISPLAYTYPE_TREE = 10,
RESOURCEDISPLAYTYPE_NDSCONTAINER = 11
}
[StructLayout(LayoutKind.Sequential)]//按顺序布局结构体成员
public struct NETRESOURCE
{
public RESOURCE_SCOPE dwScope; //枚举资源
public RESOURCE_TYPE dwType; //指定网络资源类型
public RESOURCE_DISPLAYTYPE dwDisplayType; //带有前缀RESOURCEDISPLAYTYPE的一个常数,对资源的类型进行了定义(在网络浏览器中如何显示)
public RESOURCE_USAGE dwUsage;//(可同这个资源连接 / 这个资源包含了可以枚举的额外资源)
[MarshalAs(UnmanagedType.LPStr)]//指定下面参数(lpLocalName)的属性
public string lpLocalName;//指定本地设备
[MarshalAs(UnmanagedType.LPStr)]
public string lpRemoteName;//指定远程网络名
[MarshalAs(UnmanagedType.LPStr)]
public string lpComment;//由网络供应商设置
[MarshalAs(UnmanagedType.LPStr)]
public string lpProvider;//指定网络资源供应商,为空表示供应商未知
public override String ToString()
{
String str = "LocalName: " + lpLocalName + " RemoteName: " + lpRemoteName
+ " Comment: " + lpComment + " lpProvider: " + lpProvider;
return (str);
}
}
#endregion lpNetResouce结构体
/// <summary>
/// 该类提供远程连接
/// </summary>
public class NetFile
{
public NetFile()
{
}
[DllImport("mpr.dll")]
public static extern int WNetAddConnection2A(NETRESOURCE[] lpNetResource, string lpPassword, string lpUserName, int dwFlags);
[DllImport("mpr.dll")]
public static extern int WNetCancelConnection2A(string sharename, int dwFlags, int fForce);
public static int Connect(string remotePath, string localPath,string userName,string passward)
{
NETRESOURCE[] share_driver = new NETRESOURCE[1];
share_driver[0].dwScope = RESOURCE_SCOPE.RESOURCE_GLOBALNET;//枚举所有资源
share_driver[0].dwType = RESOURCE_TYPE.RESOURCETYPE_ANY;//枚举所有
share_driver[0].dwDisplayType = RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SHARE;//枚举共享
share_driver[0].dwUsage = RESOURCE_USAGE.RESOURCEUSAGE_CONNECTABLE;//可以连接
share_driver[0].lpLocalName = localPath;
share_driver[0].lpRemoteName = remotePath;
Disconnect(localPath);
int ret;
if (userName == "" || passward == "")
{
ret = WNetAddConnection2A(share_driver, null, null, 1);
}
else
{
ret = WNetAddConnection2A(share_driver, userName, passward, 1);
}
return ret;
}
/// <summary>
/// 结束一个网络连接
///
/// </summary>
/// <param name="localpath"></param>
/// <returns></returns>
public static int Disconnect(string localpath)
{
//参数依次是:已连接资源的远程名称或本地名称;
//Long,设为零或CONNECT_UPDATE_PROFILE。如为零,而且建立的是永久性连接,则在windows下次重新启动时仍会重新连接
//Long,如为TRUE,表示强制断开连接(即使连接的资源上正有打开的文件或作业)
return WNetCancelConnection2A(localpath, 1, 1);
}
public static void TestNetWorkConnection(Form fm,string Remotepath,string userName,string pwd,string localPath)
{
Form ff = fm;
int status = Connect(Remotepath,localPath, userName, pwd);
if (status == (int)ERROR_ID.ERROR_SUCCESS)
{
IOHelper.ImportMethod(fm.ListView1);
}
else
{
MessageBox.Show("连接失败:代号--" + status + " ");
}
Disconnect(localPath);
}
}
}
读取远程文件
最新推荐文章于 2022-09-13 13:21:11 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)