123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace LYFZ.Network
- {
- #region 网络资源相关枚举
- /// <summary>
- /// 连接错误ID
- /// </summary>
- public enum ERROR_ID
- {
- /// <summary>
- /// 成功
- /// </summary>
- 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
- }
- /// <summary>
- /// 资源范围
- /// </summary>
- public enum RESOURCE_SCOPE
- {
- RESOURCE_CONNECTED = 1,
- RESOURCE_GLOBALNET = 2,
- RESOURCE_REMEMBERED = 3,
- RESOURCE_RECENT = 4,
- RESOURCE_CONTEXT = 5
- }
- /// <summary>
- /// 资源类型
- /// </summary>
- public enum RESOURCE_TYPE
- {
- RESOURCETYPE_ANY = 0,
- RESOURCETYPE_DISK = 1,
- RESOURCETYPE_PRINT = 2,
- RESOURCETYPE_RESERVED = 8,
- }
- /// <summary>
- /// 资源用法
- /// </summary>
- 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),
- }
- /// <summary>
- /// 资源显示类型
- /// </summary>
- 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
- }
- #endregion
- /// <summary>
- /// 网络连接
- /// </summary>
- public class NetworkConnection
- {
- /// <summary>
- /// NET资源结构
- /// </summary>
- [StructLayout(LayoutKind.Sequential)]
- public struct NETRESOURCE
- {
- public RESOURCE_SCOPE dwScope;
- public RESOURCE_TYPE dwType;
- public RESOURCE_DISPLAYTYPE dwDisplayType;
- public RESOURCE_USAGE dwUsage;
- [MarshalAs(UnmanagedType.LPStr)]
- public string lpLocalName;
- [MarshalAs(UnmanagedType.LPStr)]
- public string lpRemoteName;
- [MarshalAs(UnmanagedType.LPStr)]
- public string lpComment;
- [MarshalAs(UnmanagedType.LPStr)]
- public string lpProvider;
- }
- [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);
- /// <summary>
- /// 获取一个未被占用的驱动盘符号
- /// </summary>
- /// <returns></returns>
- public static string GetDriveLetter()
- {
- System.IO.DriveInfo[] drivers = System.IO.DriveInfo.GetDrives();
- string[] Letters = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
- string tempLetter = "";
- foreach (System.IO.DriveInfo disk in drivers)//逐个显示到树形目录或者文件列表中
- {
- tempLetter += disk.Name;
- }
- string Letter =null;
- for (int i = 4; i < Letters.Length; i++)
- {
- Letter = Letters[i];
- if (!tempLetter.ToLower().Contains(Letter.ToLower()))
- {
- Letter = Letter + ":";
- break;
- }
- else {
- Letter = null;
- }
- }
- return Letter;
- }
- /// <summary>
- /// 获取一个未被占用的驱动盘符号
- /// </summary>
- /// <returns></returns>
- public static string GetStaticDriveLetter()
- {
- return GetDriveLetter();
- }
- /// <summary>
- /// 连接网络共享资源
- /// </summary>
- /// <param name="remotePath">运程地址</param>
- /// <param name="username">用户名</param>
- /// <param name="password">密码</param>
- /// <param name="localPath">连接后的映射盘符,可以为空 如:“X:”</param>
- /// <returns></returns>
- public static int Connect(string remotePath, string username, string password, string localPath=null)
- {
- NETRESOURCE[] share_driver = new NETRESOURCE[1];
- share_driver[0].dwScope = RESOURCE_SCOPE.RESOURCE_GLOBALNET;
- share_driver[0].dwType = RESOURCE_TYPE.RESOURCETYPE_DISK;
- share_driver[0].dwDisplayType = RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SHARE;
- share_driver[0].dwUsage = RESOURCE_USAGE.RESOURCEUSAGE_CONNECTABLE;
- /* if (localPath != null)
- {
- share_driver[0].lpLocalName = localPath;
- }
- else {
- localPath = GetDriveLetter();
- if (localPath != null)
- {
- share_driver[0].lpLocalName = localPath;
- }
- }
- */
- share_driver[0].lpRemoteName = remotePath;
- // if (localPath != null)
- // Disconnect(localPath);
- int ret = WNetAddConnection2A(share_driver, password, username, 1);
- return ret;
- }
- /// <summary>
- /// 连接网络共享资源
- /// </summary>
- /// <param name="remotePath">运程地址</param>
- /// <param name="username">用户名</param>
- /// <param name="password">密码</param>
- /// <param name="localPath">连接后的映射盘符,可以为空 如:“X:”</param>
- /// <returns></returns>
- public static int StaticConnect(string remotePath, string username, string password, string localPath = null)
- {
- return Connect(remotePath, username, password, localPath);
- }
- /// <summary>
- /// 断开映射连接
- /// </summary>
- /// <param name="localpath">映射盘符</param>
- /// <returns></returns>
- public static int StaticDisconnect(string localpath=null)
- {
- return Disconnect(localpath);
- }
- /// <summary>
- /// 断开映射连接
- /// </summary>
- /// <param name="localpath">映射盘符</param>
- /// <returns></returns>
- public static int Disconnect(string localpath = null)
- {
-
- return WNetCancelConnection2A(localpath, 0, 1);
- }
- /// <summary>
- /// 断开所有网络资源连接
- /// </summary>
- /// <returns></returns>
- public static string Disconnect()
- {
- return StaticExecuteCmd("net use * /del /y");
- }
- /// <summary>
- /// 断开所有网络资源连接
- /// </summary>
- /// <returns></returns>
- public static string StaticDisconnect()
- {
- return Disconnect();
- }
- /// <summary>
- /// 断开指定远程路径的网络资源连接
- /// </summary>
- /// <param name="remotePath">指定远程路径(如果连接时有映射device name 设备名,请输入映射device name 如:“X:”)</param>
- /// <returns></returns>
- public static string DisconnectRemotePath(string remotePath)
- {
- return StaticExecuteCmd("net use " + remotePath + " /del /y");
- }
- /// <summary>
- /// net use 命令连接网络共享资源
- /// </summary>
- /// <param name="remotePath">运程地址</param>
- /// <param name="userName">用户名</param>
- /// <param name="Pwd">密码</param>
- /// <param name="localPath">连接后的映射盘符,可以为空 如:“X:”也可以是 “*”表示所以可用设备名</param>
- /// <returns></returns>
- public static string ComdConnect(string remotePath, string userName="", string Pwd="", string localPath=null)
- {
- return StaticExecuteCmd(GetConnectSharedComdStr(remotePath, userName, Pwd, localPath));
- }
- /// <summary>
- /// 获取连接共享命令
- /// </summary>
- /// <param name="remotePath">运程地址</param>
- /// <param name="userName">用户名</param>
- /// <param name="Pwd">密码</param>
- /// <param name="localPath">连接后的映射盘符,可以为空 如:“X:”也可以是 “*”表示所以可用设备名</param>
- /// <returns></returns>
- public static string GetConnectSharedComdStr(string remotePath, string userName = "", string Pwd = "", string localPath = null)
- {
- if (localPath == null)
- {
- localPath = "";
- }
- return ("net use " + localPath + " " + remotePath + " \"" + Pwd + "\" /user:\"" + userName + "\"");
- }
- /// <summary>
- /// net use 命令连接网络共享资源
- /// </summary>
- /// <param name="remotePath">运程地址</param>
- /// <param name="userName">用户名</param>
- /// <param name="Pwd">密码</param>
- /// <param name="localPath">连接后的映射盘符,可以为空 如:“X:”也可以是 “*”表示所以可用设备名</param>
- /// <returns></returns>
- public static string StaticComdConnect(string remotePath, string userName, string Pwd, string localPath = null) {
- return ComdConnect(remotePath, userName, Pwd, localPath);
- }
- /// <summary>
- /// 断开指定远程路径的网络资源连接
- /// </summary>
- /// <param name="remotePath">指定远程路径</param>
- /// <returns></returns>
- public static string StaticDisconnectRemotePath(string remotePath)
- {
- return DisconnectRemotePath(remotePath);
- }
- /// <summary>
- /// 执行CMD命令
- /// </summary>
- /// <param name="command">CMD命令字符串</param>
- /// <returns></returns>
- public static string StaticExecuteCmd(string command) {
- return ExecuteCmd(command);
- }
- /// <summary>
- /// 执行CMD命令
- /// </summary>
- /// <param name="command">CMD命令字符串</param>
- /// <returns></returns>
- public static string ExecuteCmd(string command)
- {
- try
- {
- string str = command;
- System.Diagnostics.Process p = new System.Diagnostics.Process();
- p.StartInfo.FileName = "cmd.exe";
- p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
- p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
- p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
- p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
- p.StartInfo.CreateNoWindow = true;//不显示程序窗口
- // p.StartInfo.WorkingDirectory = System.Environment.GetEnvironmentVariable("windir")+"\\system32\\";
- p.Start();//启动程序
- //向cmd窗口发送输入信息
- p.StandardInput.WriteLine(str + " &exit");
- //%SystemRoot%\system32\
- p.StandardInput.AutoFlush = true;
- //p.StandardInput.WriteLine("exit");
- //向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
- //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
- //获取cmd窗口的输出信息
- string output = p.StandardOutput.ReadToEnd();
- //StreamReader reader = p.StandardOutput;
- //string line=reader.ReadLine();
- //while (!reader.EndOfStream)
- //{
- // str += line + " ";
- // line = reader.ReadLine();
- //}
- p.WaitForExit();//等待程序执行完退出进程
- p.Close();
- return output;
- }
- catch (Exception ex){
- return "命令执行失败:"+ex.Message;
- }
- }
-
-
- }
- }
|