From 48facb797ed912e4ea6b04b17d1ff190ac2daac4 Mon Sep 17 00:00:00 2001 From: stefan Date: Wed, 12 Sep 2018 19:26:21 +0200 Subject: Update to 3.5.2 and .net core 2.1 --- .../Networking/NativeMethods.cs | 226 -------- .../Networking/NetworkManager.cs | 175 ------ .../Networking/NetworkShares.cs | 643 --------------------- 3 files changed, 1044 deletions(-) delete mode 100644 MediaBrowser.ServerApplication/Networking/NativeMethods.cs delete mode 100644 MediaBrowser.ServerApplication/Networking/NetworkManager.cs delete mode 100644 MediaBrowser.ServerApplication/Networking/NetworkShares.cs (limited to 'MediaBrowser.ServerApplication/Networking') diff --git a/MediaBrowser.ServerApplication/Networking/NativeMethods.cs b/MediaBrowser.ServerApplication/Networking/NativeMethods.cs deleted file mode 100644 index 037b1f75b5..0000000000 --- a/MediaBrowser.ServerApplication/Networking/NativeMethods.cs +++ /dev/null @@ -1,226 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using System.Security; - -namespace MediaBrowser.ServerApplication.Networking -{ - /// - /// Class NativeMethods - /// - [SuppressUnmanagedCodeSecurity] - public static class NativeMethods - { - //declare the Netapi32 : NetServerEnum method import - /// - /// Nets the server enum. - /// - /// Name of the server. - /// The dw level. - /// The p buf. - /// The dw pref max len. - /// The dw entries read. - /// The dw total entries. - /// Type of the dw server. - /// The domain. - /// The dw resume handle. - /// System.Int32. - [DllImport("Netapi32", CharSet = CharSet.Auto, SetLastError = true), - SuppressUnmanagedCodeSecurity] - - public static extern int NetServerEnum( - string ServerName, // must be null - int dwLevel, - ref IntPtr pBuf, - int dwPrefMaxLen, - out int dwEntriesRead, - out int dwTotalEntries, - int dwServerType, - string domain, // null for login domain - out int dwResumeHandle - ); - - //declare the Netapi32 : NetApiBufferFree method import - /// - /// Nets the API buffer free. - /// - /// The p buf. - /// System.Int32. - [DllImport("Netapi32", SetLastError = true), - SuppressUnmanagedCodeSecurity] - public static extern int NetApiBufferFree( - IntPtr pBuf); - - [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool FileTimeToSystemTime( - [In] ref long fileTime, - out SystemTime systemTime); - - [DllImport("AdvApi32.dll", SetLastError = true, ExactSpelling = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool CryptAcquireContextW( - out IntPtr providerContext, - [MarshalAs(UnmanagedType.LPWStr)] string container, - [MarshalAs(UnmanagedType.LPWStr)] string provider, - int providerType, - int flags); - - [DllImport("AdvApi32.dll", SetLastError = true, ExactSpelling = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool CryptReleaseContext( - IntPtr providerContext, - int flags); - - [DllImport("AdvApi32.dll", SetLastError = true, ExactSpelling = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool CryptGenKey( - IntPtr providerContext, - int algorithmId, - int flags, - out IntPtr cryptKeyHandle); - - [DllImport("AdvApi32.dll", SetLastError = true, ExactSpelling = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool CryptDestroyKey( - IntPtr cryptKeyHandle); - - [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool CertStrToNameW( - int certificateEncodingType, - IntPtr x500, - int strType, - IntPtr reserved, - [MarshalAs(UnmanagedType.LPArray)] [Out] byte[] encoded, - ref int encodedLength, - out IntPtr errorString); - - [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)] - public static extern IntPtr CertCreateSelfSignCertificate( - IntPtr providerHandle, - [In] ref CryptoApiBlob subjectIssuerBlob, - int flags, - [In] ref CryptKeyProviderInformation keyProviderInformation, - [In] ref CryptAlgorithmIdentifier algorithmIdentifier, - [In] ref SystemTime startTime, - [In] ref SystemTime endTime, - IntPtr extensions); - - [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool CertFreeCertificateContext( - IntPtr certificateContext); - - [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)] - public static extern IntPtr CertOpenStore( - [MarshalAs(UnmanagedType.LPStr)] string storeProvider, - int messageAndCertificateEncodingType, - IntPtr cryptProvHandle, - int flags, - IntPtr parameters); - - [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool CertCloseStore( - IntPtr certificateStoreHandle, - int flags); - - [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool CertAddCertificateContextToStore( - IntPtr certificateStoreHandle, - IntPtr certificateContext, - int addDisposition, - out IntPtr storeContextPtr); - - [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool CertSetCertificateContextProperty( - IntPtr certificateContext, - int propertyId, - int flags, - [In] ref CryptKeyProviderInformation data); - - [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool PFXExportCertStoreEx( - IntPtr certificateStoreHandle, - ref CryptoApiBlob pfxBlob, - IntPtr password, - IntPtr reserved, - int flags); - } - - //create a _SERVER_INFO_100 STRUCTURE - /// - /// Struct _SERVER_INFO_100 - /// - [StructLayout(LayoutKind.Sequential)] - public struct _SERVER_INFO_100 - { - /// - /// The sv100_platform_id - /// - internal int sv100_platform_id; - /// - /// The sv100_name - /// - [MarshalAs(UnmanagedType.LPWStr)] - internal string sv100_name; - } - - [StructLayout(LayoutKind.Sequential)] - public struct SystemTime - { - public short Year; - public short Month; - public short DayOfWeek; - public short Day; - public short Hour; - public short Minute; - public short Second; - public short Milliseconds; - } - - [StructLayout(LayoutKind.Sequential)] - public struct CryptObjIdBlob - { - public uint cbData; - public IntPtr pbData; - } - - [StructLayout(LayoutKind.Sequential)] - public struct CryptAlgorithmIdentifier - { - [MarshalAs(UnmanagedType.LPStr)] - public String pszObjId; - public CryptObjIdBlob Parameters; - } - - [StructLayout(LayoutKind.Sequential)] - public struct CryptoApiBlob - { - public int DataLength; - public IntPtr Data; - - public CryptoApiBlob(int dataLength, IntPtr data) - { - this.DataLength = dataLength; - this.Data = data; - } - } - - [StructLayout(LayoutKind.Sequential)] - public struct CryptKeyProviderInformation - { - [MarshalAs(UnmanagedType.LPWStr)] - public string ContainerName; - [MarshalAs(UnmanagedType.LPWStr)] - public string ProviderName; - public int ProviderType; - public int Flags; - public int ProviderParameterCount; - public IntPtr ProviderParameters; // PCRYPT_KEY_PROV_PARAM - public int KeySpec; - } -} diff --git a/MediaBrowser.ServerApplication/Networking/NetworkManager.cs b/MediaBrowser.ServerApplication/Networking/NetworkManager.cs deleted file mode 100644 index 8933a5760d..0000000000 --- a/MediaBrowser.ServerApplication/Networking/NetworkManager.cs +++ /dev/null @@ -1,175 +0,0 @@ -using MediaBrowser.Model.IO; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Net; -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices; - -namespace MediaBrowser.ServerApplication.Networking -{ - /// - /// Class NetUtils - /// - public class NetworkManager : Emby.Server.Implementations.Networking.NetworkManager - { - public NetworkManager(ILogger logger) - : base(logger) - { - } - - /// - /// Gets the network shares. - /// - /// The path. - /// IEnumerable{NetworkShare}. - public override IEnumerable GetNetworkShares(string path) - { - Logger.Info("Getting network shares from {0}", path); - return new ShareCollection(path).OfType().Select(ToNetworkShare); - } - - /// - /// To the network share. - /// - /// The share. - /// NetworkShare. - private NetworkShare ToNetworkShare(Share share) - { - return new NetworkShare - { - Name = share.NetName, - Path = share.Path, - Remark = share.Remark, - Server = share.Server, - ShareType = ToNetworkShareType(share.ShareType) - }; - } - - /// - /// To the type of the network share. - /// - /// Type of the share. - /// NetworkShareType. - /// Unknown share type - private NetworkShareType ToNetworkShareType(ShareType shareType) - { - if (shareType.HasFlag(ShareType.Special)) - { - return NetworkShareType.Special; - } - if (shareType.HasFlag(ShareType.Device)) - { - return NetworkShareType.Device; - } - if (shareType.HasFlag(ShareType.Disk)) - { - return NetworkShareType.Disk; - } - if (shareType.HasFlag(ShareType.IPC)) - { - return NetworkShareType.Ipc; - } - if (shareType.HasFlag(ShareType.Printer)) - { - return NetworkShareType.Printer; - } - throw new ArgumentException("Unknown share type"); - } - - /// - /// Uses the DllImport : NetServerEnum with all its required parameters - /// (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp - /// for full details or method signature) to retrieve a list of domain SV_TYPE_WORKSTATION - /// and SV_TYPE_SERVER PC's - /// - /// Arraylist that represents all the SV_TYPE_WORKSTATION and SV_TYPE_SERVER - /// PC's in the Domain - private List GetNetworkDevicesInternal() - { - //local fields - const int MAX_PREFERRED_LENGTH = -1; - var SV_TYPE_WORKSTATION = 1; - var SV_TYPE_SERVER = 2; - IntPtr buffer = IntPtr.Zero; - IntPtr tmpBuffer = IntPtr.Zero; - var entriesRead = 0; - var totalEntries = 0; - var resHandle = 0; - var sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100)); - - var returnList = new List(); - - try - { - //call the DllImport : NetServerEnum with all its required parameters - //see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp - //for full details of method signature - var ret = NativeMethods.NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle); - - //if the returned with a NERR_Success (C++ term), =0 for C# - if (ret == 0) - { - //loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's - for (var i = 0; i < totalEntries; i++) - { - //get pointer to, Pointer to the buffer that received the data from - //the call to NetServerEnum. Must ensure to use correct size of - //STRUCTURE to ensure correct location in memory is pointed to - tmpBuffer = new IntPtr((Int64)buffer + (i * sizeofINFO)); - //Have now got a pointer to the list of SV_TYPE_WORKSTATION and - //SV_TYPE_SERVER PC's, which is unmanaged memory - //Needs to Marshal data from an unmanaged block of memory to a - //managed object, again using STRUCTURE to ensure the correct data - //is marshalled - var svrInfo = (_SERVER_INFO_100)Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100)); - - //add the PC names to the ArrayList - if (!string.IsNullOrEmpty(svrInfo.sv100_name)) - { - returnList.Add(svrInfo.sv100_name); - } - } - } - } - finally - { - //The NetApiBufferFree function frees - //the memory that the NetApiBufferAllocate function allocates - NativeMethods.NetApiBufferFree(buffer); - } - - return returnList; - } - - /// - /// Gets available devices within the domain - /// - /// PC's in the Domain - public override IEnumerable GetNetworkDevices() - { - return GetNetworkDevicesInternal().Select(c => new FileSystemEntryInfo - { - Name = c, - Path = NetworkPrefix + c, - Type = FileSystemEntryType.NetworkComputer - }); - } - - /// - /// Gets the network prefix. - /// - /// The network prefix. - private string NetworkPrefix - { - get - { - var separator = Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture); - return separator + separator; - } - } - } - -} diff --git a/MediaBrowser.ServerApplication/Networking/NetworkShares.cs b/MediaBrowser.ServerApplication/Networking/NetworkShares.cs deleted file mode 100644 index f9a59203d7..0000000000 --- a/MediaBrowser.ServerApplication/Networking/NetworkShares.cs +++ /dev/null @@ -1,643 +0,0 @@ -using System; -using System.IO; -using System.Collections; -using System.Runtime.InteropServices; - -namespace MediaBrowser.ServerApplication.Networking -{ - /// - /// Type of share - /// - [Flags] - public enum ShareType - { - /// Disk share - Disk = 0, - /// Printer share - Printer = 1, - /// Device share - Device = 2, - /// IPC share - IPC = 3, - /// Special share - Special = -2147483648, // 0x80000000, - } - - #region Share - - /// - /// Information about a local share - /// - public class Share - { - #region Private data - - private string _server; - private string _netName; - private string _path; - private ShareType _shareType; - private string _remark; - - #endregion - - #region Constructor - - /// - /// Constructor - /// - /// The server. - /// Name of the net. - /// The path. - /// Type of the share. - /// The remark. - public Share(string server, string netName, string path, ShareType shareType, string remark) - { - if (ShareType.Special == shareType && "IPC$" == netName) - { - shareType |= ShareType.IPC; - } - - _server = server; - _netName = netName; - _path = path; - _shareType = shareType; - _remark = remark; - } - - #endregion - - #region Properties - - /// - /// The name of the computer that this share belongs to - /// - public string Server - { - get { return _server; } - } - - /// - /// Share name - /// - public string NetName - { - get { return _netName; } - } - - /// - /// Local path - /// - public string Path - { - get { return _path; } - } - - /// - /// Share type - /// - public ShareType ShareType - { - get { return _shareType; } - } - - /// - /// Comment - /// - public string Remark - { - get { return _remark; } - } - - /// - /// Returns true if this is a file system share - /// - public bool IsFileSystem - { - get - { - // Shared device - if (0 != (_shareType & ShareType.Device)) return false; - // IPC share - if (0 != (_shareType & ShareType.IPC)) return false; - // Shared printer - if (0 != (_shareType & ShareType.Printer)) return false; - - // Standard disk share - if (0 == (_shareType & ShareType.Special)) return true; - - // Special disk share (e.g. C$) - return ShareType.Special == _shareType && !string.IsNullOrEmpty(_netName); - } - } - - /// - /// Get the root of a disk-based share - /// - public DirectoryInfo Root - { - get - { - if (IsFileSystem) - { - if (string.IsNullOrEmpty(_server)) - if (string.IsNullOrEmpty(_path)) - return new DirectoryInfo(ToString()); - else - return new DirectoryInfo(_path); - return new DirectoryInfo(ToString()); - } - return null; - } - } - - #endregion - - /// - /// Returns the path to this share - /// - /// - public override string ToString() - { - if (string.IsNullOrEmpty(_server)) - { - return string.Format(@"\\{0}\{1}", Environment.MachineName, _netName); - } - return string.Format(@"\\{0}\{1}", _server, _netName); - } - - /// - /// Returns true if this share matches the local path - /// - /// - /// - public bool MatchesPath(string path) - { - if (!IsFileSystem) return false; - if (string.IsNullOrEmpty(path)) return true; - - return path.ToLower().StartsWith(_path.ToLower()); - } - } - - #endregion - - /// - /// A collection of shares - /// - public class ShareCollection : ReadOnlyCollectionBase - { - #region Platform - - /// - /// Is this an NT platform? - /// - protected static bool IsNT - { - get { return (PlatformID.Win32NT == Environment.OSVersion.Platform); } - } - - /// - /// Returns true if this is Windows 2000 or higher - /// - protected static bool IsW2KUp - { - get - { - OperatingSystem os = Environment.OSVersion; - if (PlatformID.Win32NT == os.Platform && os.Version.Major >= 5) - return true; - else - return false; - } - } - - #endregion - - #region Interop - - #region Constants - - /// Maximum path length - protected const int MAX_PATH = 260; - /// No error - protected const int NO_ERROR = 0; - /// Access denied - protected const int ERROR_ACCESS_DENIED = 5; - /// Access denied - protected const int ERROR_WRONG_LEVEL = 124; - /// More data available - protected const int ERROR_MORE_DATA = 234; - /// Not connected - protected const int ERROR_NOT_CONNECTED = 2250; - /// Level 1 - protected const int UNIVERSAL_NAME_INFO_LEVEL = 1; - /// Max extries (9x) - protected const int MAX_SI50_ENTRIES = 20; - - #endregion - - #region Structures - - /// Unc name - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - protected struct UNIVERSAL_NAME_INFO - { - [MarshalAs(UnmanagedType.LPTStr)] - public string lpUniversalName; - } - - /// Share information, NT, level 2 - /// - /// Requires admin rights to work. - /// - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - protected struct SHARE_INFO_2 - { - [MarshalAs(UnmanagedType.LPWStr)] - public string NetName; - public ShareType ShareType; - [MarshalAs(UnmanagedType.LPWStr)] - public string Remark; - public int Permissions; - public int MaxUsers; - public int CurrentUsers; - [MarshalAs(UnmanagedType.LPWStr)] - public string Path; - [MarshalAs(UnmanagedType.LPWStr)] - public string Password; - } - - /// Share information, NT, level 1 - /// - /// Fallback when no admin rights. - /// - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - protected struct SHARE_INFO_1 - { - [MarshalAs(UnmanagedType.LPWStr)] - public string NetName; - public ShareType ShareType; - [MarshalAs(UnmanagedType.LPWStr)] - public string Remark; - } - - /// Share information, Win9x - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] - protected struct SHARE_INFO_50 - { - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)] - public string NetName; - - public byte bShareType; - public ushort Flags; - - [MarshalAs(UnmanagedType.LPTStr)] - public string Remark; - [MarshalAs(UnmanagedType.LPTStr)] - public string Path; - - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)] - public string PasswordRW; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)] - public string PasswordRO; - - public ShareType ShareType - { - get { return (ShareType)((int)bShareType & 0x7F); } - } - } - - /// Share information level 1, Win9x - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] - protected struct SHARE_INFO_1_9x - { - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)] - public string NetName; - public byte Padding; - - public ushort bShareType; - - [MarshalAs(UnmanagedType.LPTStr)] - public string Remark; - - public ShareType ShareType - { - get { return (ShareType)((int)bShareType & 0x7FFF); } - } - } - - #endregion - - #region Functions - - /// Get a UNC name - [DllImport("mpr", CharSet = CharSet.Auto)] - protected static extern int WNetGetUniversalName(string lpLocalPath, - int dwInfoLevel, ref UNIVERSAL_NAME_INFO lpBuffer, ref int lpBufferSize); - - /// Get a UNC name - [DllImport("mpr", CharSet = CharSet.Auto)] - protected static extern int WNetGetUniversalName(string lpLocalPath, - int dwInfoLevel, IntPtr lpBuffer, ref int lpBufferSize); - - /// Enumerate shares (NT) - [DllImport("netapi32", CharSet = CharSet.Unicode)] - protected static extern int NetShareEnum(string lpServerName, int dwLevel, - out IntPtr lpBuffer, int dwPrefMaxLen, out int entriesRead, - out int totalEntries, ref int hResume); - - /// Enumerate shares (9x) - [DllImport("svrapi", CharSet = CharSet.Ansi)] - protected static extern int NetShareEnum( - [MarshalAs(UnmanagedType.LPTStr)] string lpServerName, int dwLevel, - IntPtr lpBuffer, ushort cbBuffer, out ushort entriesRead, - out ushort totalEntries); - - /// Free the buffer (NT) - [DllImport("netapi32")] - protected static extern int NetApiBufferFree(IntPtr lpBuffer); - - #endregion - - #region Enumerate shares - - /// - /// Enumerates the shares on Windows NT - /// - /// The server name - /// The ShareCollection - protected static void EnumerateSharesNT(string server, ShareCollection shares) - { - int level = 2; - int entriesRead, totalEntries, nRet, hResume = 0; - IntPtr pBuffer = IntPtr.Zero; - - try - { - nRet = NetShareEnum(server, level, out pBuffer, -1, - out entriesRead, out totalEntries, ref hResume); - - if (ERROR_ACCESS_DENIED == nRet) - { - //Need admin for level 2, drop to level 1 - level = 1; - nRet = NetShareEnum(server, level, out pBuffer, -1, - out entriesRead, out totalEntries, ref hResume); - } - - if (NO_ERROR == nRet && entriesRead > 0) - { - Type t = (2 == level) ? typeof(SHARE_INFO_2) : typeof(SHARE_INFO_1); - int offset = Marshal.SizeOf(t); - - var lpItem = pBuffer.ToInt64(); - - for (int i = 0; i < entriesRead; i++, lpItem += offset) - { - IntPtr pItem = new IntPtr(lpItem); - if (1 == level) - { - SHARE_INFO_1 si = (SHARE_INFO_1)Marshal.PtrToStructure(pItem, t); - shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark); - } - else - { - SHARE_INFO_2 si = (SHARE_INFO_2)Marshal.PtrToStructure(pItem, t); - shares.Add(si.NetName, si.Path, si.ShareType, si.Remark); - } - } - } - - } - finally - { - // Clean up buffer allocated by system - if (IntPtr.Zero != pBuffer) - NetApiBufferFree(pBuffer); - } - } - - /// - /// Enumerates the shares on Windows 9x - /// - /// The server name - /// The ShareCollection - protected static void EnumerateShares9x(string server, ShareCollection shares) - { - int level = 50; - int nRet = 0; - ushort entriesRead, totalEntries; - - var t = typeof(SHARE_INFO_50); - var size = Marshal.SizeOf(t); - var cbBuffer = (ushort)(MAX_SI50_ENTRIES * size); - //On Win9x, must allocate buffer before calling API - IntPtr pBuffer = Marshal.AllocHGlobal(cbBuffer); - - try - { - nRet = NetShareEnum(server, level, pBuffer, cbBuffer, - out entriesRead, out totalEntries); - - if (ERROR_WRONG_LEVEL == nRet) - { - level = 1; - t = typeof(SHARE_INFO_1_9x); - size = Marshal.SizeOf(t); - - nRet = NetShareEnum(server, level, pBuffer, cbBuffer, - out entriesRead, out totalEntries); - } - - if (NO_ERROR == nRet || ERROR_MORE_DATA == nRet) - { - for (int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += size) - { - var pItem = new IntPtr(lpItem); - - if (1 == level) - { - var si = (SHARE_INFO_1_9x)Marshal.PtrToStructure(pItem, t); - shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark); - } - else - { - var si = (SHARE_INFO_50)Marshal.PtrToStructure(pItem, t); - shares.Add(si.NetName, si.Path, si.ShareType, si.Remark); - } - } - } - else - Console.WriteLine(nRet); - - } - finally - { - //Clean up buffer - Marshal.FreeHGlobal(pBuffer); - } - } - - /// - /// Enumerates the shares - /// - /// The server name - /// The ShareCollection - protected static void EnumerateShares(string server, ShareCollection shares) - { - if (null != server && 0 != server.Length && !IsW2KUp) - { - server = server.ToUpper(); - - // On NT4, 9x and Me, server has to start with "\\" - if (!('\\' == server[0] && '\\' == server[1])) - server = @"\\" + server; - } - - if (IsNT) - EnumerateSharesNT(server, shares); - else - EnumerateShares9x(server, shares); - } - - #endregion - - #endregion - - #region Static methods - - /// - /// Returns true if fileName is a valid local file-name of the form: - /// X:\, where X is a drive letter from A-Z - /// - /// The filename to check - /// - public static bool IsValidFilePath(string fileName) - { - if (null == fileName || 0 == fileName.Length) return false; - - char drive = char.ToUpper(fileName[0]); - if ('A' > drive || drive > 'Z') - return false; - - else if (Path.VolumeSeparatorChar != fileName[1]) - return false; - else if (Path.DirectorySeparatorChar != fileName[2]) - return false; - else - return true; - } - - #endregion - - /// The name of the server this collection represents - private string _server; - - #region Constructor - - /// - /// Default constructor - local machine - /// - public ShareCollection() - { - _server = string.Empty; - EnumerateShares(_server, this); - } - - /// - /// Constructor - /// - /// The server. - public ShareCollection(string server) - { - _server = server; - EnumerateShares(_server, this); - } - - #endregion - - #region Add - - protected void Add(Share share) - { - InnerList.Add(share); - } - - protected void Add(string netName, string path, ShareType shareType, string remark) - { - InnerList.Add(new Share(_server, netName, path, shareType, remark)); - } - - #endregion - - #region Properties - - /// - /// Returns the name of the server this collection represents - /// - public string Server - { - get { return _server; } - } - - /// - /// Returns the at the specified index. - /// - public Share this[int index] - { - get { return (Share)InnerList[index]; } - } - - /// - /// Returns the which matches a given local path - /// - /// The path to match - public Share this[string path] - { - get - { - if (null == path || 0 == path.Length) return null; - - path = Path.GetFullPath(path); - if (!IsValidFilePath(path)) return null; - - Share match = null; - - foreach (object t in InnerList) - { - var s = (Share)t; - - if (s.IsFileSystem && s.MatchesPath(path)) - { - //Store first match - if (null == match) - match = s; - - // If this has a longer path, - // and this is a disk share or match is a special share, - // then this is a better match - else if (match.Path.Length < s.Path.Length) - { - if (ShareType.Disk == s.ShareType || ShareType.Disk != match.ShareType) - match = s; - } - } - } - - return match; - } - } - - #endregion - - /// - /// Copy this collection to an array - /// - /// - /// - public void CopyTo(Share[] array, int index) - { - InnerList.CopyTo(array, index); - } - } -} \ No newline at end of file -- cgit v1.2.3