123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace LYFZ.Network
- {
- #region 网络资源相关枚举
-
-
-
- public enum ERROR_ID
- {
-
-
-
- ERROR_SUCCESS = 0,
- 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
- }
- #endregion
-
-
-
- public class NetworkConnection
- {
-
-
-
- [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);
-
-
-
-
- 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;
- }
-
-
-
-
- public static string GetStaticDriveLetter()
- {
- return GetDriveLetter();
- }
-
-
-
-
-
-
-
-
- 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;
-
- share_driver[0].lpRemoteName = remotePath;
-
-
- int ret = WNetAddConnection2A(share_driver, password, username, 1);
- return ret;
- }
-
-
-
-
-
-
-
-
- public static int StaticConnect(string remotePath, string username, string password, string localPath = null)
- {
- return Connect(remotePath, username, password, localPath);
- }
-
-
-
-
-
- public static int StaticDisconnect(string localpath=null)
- {
- return Disconnect(localpath);
- }
-
-
-
-
-
- public static int Disconnect(string localpath = null)
- {
-
- return WNetCancelConnection2A(localpath, 0, 1);
- }
-
-
-
-
- public static string Disconnect()
- {
- return StaticExecuteCmd("net use * /del /y");
- }
-
-
-
-
- public static string StaticDisconnect()
- {
- return Disconnect();
- }
-
-
-
-
-
- public static string DisconnectRemotePath(string remotePath)
- {
- return StaticExecuteCmd("net use " + remotePath + " /del /y");
- }
-
-
-
-
-
-
-
-
- public static string ComdConnect(string remotePath, string userName="", string Pwd="", string localPath=null)
- {
- return StaticExecuteCmd(GetConnectSharedComdStr(remotePath, userName, Pwd, localPath));
- }
-
-
-
-
-
-
-
-
- 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 + "\"");
- }
-
-
-
-
-
-
-
-
- public static string StaticComdConnect(string remotePath, string userName, string Pwd, string localPath = null) {
- return ComdConnect(remotePath, userName, Pwd, localPath);
- }
-
-
-
-
-
- public static string StaticDisconnectRemotePath(string remotePath)
- {
- return DisconnectRemotePath(remotePath);
- }
-
-
-
-
-
- public static string StaticExecuteCmd(string command) {
- return ExecuteCmd(command);
- }
-
-
-
-
-
- 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;
- p.StartInfo.RedirectStandardInput = true;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.RedirectStandardError = true;
- p.StartInfo.CreateNoWindow = true;
-
- p.Start();
-
- p.StandardInput.WriteLine(str + " &exit");
-
- p.StandardInput.AutoFlush = true;
-
-
-
-
- string output = p.StandardOutput.ReadToEnd();
-
-
-
-
-
-
-
- p.WaitForExit();
- p.Close();
- return output;
- }
- catch (Exception ex){
- return "命令执行失败:"+ex.Message;
- }
- }
-
-
- }
- }
|