NetworkShares.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. namespace MediaBrowser.Common.Implementations.NetworkManagement
  6. {
  7. /// <summary>
  8. /// Type of share
  9. /// </summary>
  10. [Flags]
  11. public enum ShareType
  12. {
  13. /// <summary>Disk share</summary>
  14. Disk = 0,
  15. /// <summary>Printer share</summary>
  16. Printer = 1,
  17. /// <summary>Device share</summary>
  18. Device = 2,
  19. /// <summary>IPC share</summary>
  20. IPC = 3,
  21. /// <summary>Special share</summary>
  22. Special = -2147483648, // 0x80000000,
  23. }
  24. #region Share
  25. /// <summary>
  26. /// Information about a local share
  27. /// </summary>
  28. public class Share
  29. {
  30. #region Private data
  31. private string _server;
  32. private string _netName;
  33. private string _path;
  34. private ShareType _shareType;
  35. private string _remark;
  36. #endregion
  37. #region Constructor
  38. /// <summary>
  39. /// Constructor
  40. /// </summary>
  41. /// <param name="server">The server.</param>
  42. /// <param name="netName">Name of the net.</param>
  43. /// <param name="path">The path.</param>
  44. /// <param name="shareType">Type of the share.</param>
  45. /// <param name="remark">The remark.</param>
  46. public Share(string server, string netName, string path, ShareType shareType, string remark)
  47. {
  48. if (ShareType.Special == shareType && "IPC$" == netName)
  49. {
  50. shareType |= ShareType.IPC;
  51. }
  52. _server = server;
  53. _netName = netName;
  54. _path = path;
  55. _shareType = shareType;
  56. _remark = remark;
  57. }
  58. #endregion
  59. #region Properties
  60. /// <summary>
  61. /// The name of the computer that this share belongs to
  62. /// </summary>
  63. public string Server
  64. {
  65. get { return _server; }
  66. }
  67. /// <summary>
  68. /// Share name
  69. /// </summary>
  70. public string NetName
  71. {
  72. get { return _netName; }
  73. }
  74. /// <summary>
  75. /// Local path
  76. /// </summary>
  77. public string Path
  78. {
  79. get { return _path; }
  80. }
  81. /// <summary>
  82. /// Share type
  83. /// </summary>
  84. public ShareType ShareType
  85. {
  86. get { return _shareType; }
  87. }
  88. /// <summary>
  89. /// Comment
  90. /// </summary>
  91. public string Remark
  92. {
  93. get { return _remark; }
  94. }
  95. /// <summary>
  96. /// Returns true if this is a file system share
  97. /// </summary>
  98. public bool IsFileSystem
  99. {
  100. get
  101. {
  102. // Shared device
  103. if (0 != (_shareType & ShareType.Device)) return false;
  104. // IPC share
  105. if (0 != (_shareType & ShareType.IPC)) return false;
  106. // Shared printer
  107. if (0 != (_shareType & ShareType.Printer)) return false;
  108. // Standard disk share
  109. if (0 == (_shareType & ShareType.Special)) return true;
  110. // Special disk share (e.g. C$)
  111. return ShareType.Special == _shareType && !string.IsNullOrEmpty(_netName);
  112. }
  113. }
  114. /// <summary>
  115. /// Get the root of a disk-based share
  116. /// </summary>
  117. public DirectoryInfo Root
  118. {
  119. get
  120. {
  121. if (IsFileSystem)
  122. {
  123. if (string.IsNullOrEmpty(_server))
  124. if (string.IsNullOrEmpty(_path))
  125. return new DirectoryInfo(ToString());
  126. else
  127. return new DirectoryInfo(_path);
  128. return new DirectoryInfo(ToString());
  129. }
  130. return null;
  131. }
  132. }
  133. #endregion
  134. /// <summary>
  135. /// Returns the path to this share
  136. /// </summary>
  137. /// <returns></returns>
  138. public override string ToString()
  139. {
  140. if (string.IsNullOrEmpty(_server))
  141. {
  142. return string.Format(@"\\{0}\{1}", Environment.MachineName, _netName);
  143. }
  144. return string.Format(@"\\{0}\{1}", _server, _netName);
  145. }
  146. /// <summary>
  147. /// Returns true if this share matches the local path
  148. /// </summary>
  149. /// <param name="path"></param>
  150. /// <returns></returns>
  151. public bool MatchesPath(string path)
  152. {
  153. if (!IsFileSystem) return false;
  154. if (string.IsNullOrEmpty(path)) return true;
  155. return path.ToLower().StartsWith(_path.ToLower());
  156. }
  157. }
  158. #endregion
  159. /// <summary>
  160. /// A collection of shares
  161. /// </summary>
  162. public class ShareCollection : ReadOnlyCollectionBase
  163. {
  164. #region Platform
  165. /// <summary>
  166. /// Is this an NT platform?
  167. /// </summary>
  168. protected static bool IsNT
  169. {
  170. get { return (PlatformID.Win32NT == Environment.OSVersion.Platform); }
  171. }
  172. /// <summary>
  173. /// Returns true if this is Windows 2000 or higher
  174. /// </summary>
  175. protected static bool IsW2KUp
  176. {
  177. get
  178. {
  179. OperatingSystem os = Environment.OSVersion;
  180. if (PlatformID.Win32NT == os.Platform && os.Version.Major >= 5)
  181. return true;
  182. else
  183. return false;
  184. }
  185. }
  186. #endregion
  187. #region Interop
  188. #region Constants
  189. /// <summary>Maximum path length</summary>
  190. protected const int MAX_PATH = 260;
  191. /// <summary>No error</summary>
  192. protected const int NO_ERROR = 0;
  193. /// <summary>Access denied</summary>
  194. protected const int ERROR_ACCESS_DENIED = 5;
  195. /// <summary>Access denied</summary>
  196. protected const int ERROR_WRONG_LEVEL = 124;
  197. /// <summary>More data available</summary>
  198. protected const int ERROR_MORE_DATA = 234;
  199. /// <summary>Not connected</summary>
  200. protected const int ERROR_NOT_CONNECTED = 2250;
  201. /// <summary>Level 1</summary>
  202. protected const int UNIVERSAL_NAME_INFO_LEVEL = 1;
  203. /// <summary>Max extries (9x)</summary>
  204. protected const int MAX_SI50_ENTRIES = 20;
  205. #endregion
  206. #region Structures
  207. /// <summary>Unc name</summary>
  208. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  209. protected struct UNIVERSAL_NAME_INFO
  210. {
  211. [MarshalAs(UnmanagedType.LPTStr)]
  212. public string lpUniversalName;
  213. }
  214. /// <summary>Share information, NT, level 2</summary>
  215. /// <remarks>
  216. /// Requires admin rights to work.
  217. /// </remarks>
  218. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  219. protected struct SHARE_INFO_2
  220. {
  221. [MarshalAs(UnmanagedType.LPWStr)]
  222. public string NetName;
  223. public ShareType ShareType;
  224. [MarshalAs(UnmanagedType.LPWStr)]
  225. public string Remark;
  226. public int Permissions;
  227. public int MaxUsers;
  228. public int CurrentUsers;
  229. [MarshalAs(UnmanagedType.LPWStr)]
  230. public string Path;
  231. [MarshalAs(UnmanagedType.LPWStr)]
  232. public string Password;
  233. }
  234. /// <summary>Share information, NT, level 1</summary>
  235. /// <remarks>
  236. /// Fallback when no admin rights.
  237. /// </remarks>
  238. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  239. protected struct SHARE_INFO_1
  240. {
  241. [MarshalAs(UnmanagedType.LPWStr)]
  242. public string NetName;
  243. public ShareType ShareType;
  244. [MarshalAs(UnmanagedType.LPWStr)]
  245. public string Remark;
  246. }
  247. /// <summary>Share information, Win9x</summary>
  248. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
  249. protected struct SHARE_INFO_50
  250. {
  251. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
  252. public string NetName;
  253. public byte bShareType;
  254. public ushort Flags;
  255. [MarshalAs(UnmanagedType.LPTStr)]
  256. public string Remark;
  257. [MarshalAs(UnmanagedType.LPTStr)]
  258. public string Path;
  259. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
  260. public string PasswordRW;
  261. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
  262. public string PasswordRO;
  263. public ShareType ShareType
  264. {
  265. get { return (ShareType)((int)bShareType & 0x7F); }
  266. }
  267. }
  268. /// <summary>Share information level 1, Win9x</summary>
  269. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
  270. protected struct SHARE_INFO_1_9x
  271. {
  272. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
  273. public string NetName;
  274. public byte Padding;
  275. public ushort bShareType;
  276. [MarshalAs(UnmanagedType.LPTStr)]
  277. public string Remark;
  278. public ShareType ShareType
  279. {
  280. get { return (ShareType)((int)bShareType & 0x7FFF); }
  281. }
  282. }
  283. #endregion
  284. #region Functions
  285. /// <summary>Get a UNC name</summary>
  286. [DllImport("mpr", CharSet = CharSet.Auto)]
  287. protected static extern int WNetGetUniversalName(string lpLocalPath,
  288. int dwInfoLevel, ref UNIVERSAL_NAME_INFO lpBuffer, ref int lpBufferSize);
  289. /// <summary>Get a UNC name</summary>
  290. [DllImport("mpr", CharSet = CharSet.Auto)]
  291. protected static extern int WNetGetUniversalName(string lpLocalPath,
  292. int dwInfoLevel, IntPtr lpBuffer, ref int lpBufferSize);
  293. /// <summary>Enumerate shares (NT)</summary>
  294. [DllImport("netapi32", CharSet = CharSet.Unicode)]
  295. protected static extern int NetShareEnum(string lpServerName, int dwLevel,
  296. out IntPtr lpBuffer, int dwPrefMaxLen, out int entriesRead,
  297. out int totalEntries, ref int hResume);
  298. /// <summary>Enumerate shares (9x)</summary>
  299. [DllImport("svrapi", CharSet = CharSet.Ansi)]
  300. protected static extern int NetShareEnum(
  301. [MarshalAs(UnmanagedType.LPTStr)] string lpServerName, int dwLevel,
  302. IntPtr lpBuffer, ushort cbBuffer, out ushort entriesRead,
  303. out ushort totalEntries);
  304. /// <summary>Free the buffer (NT)</summary>
  305. [DllImport("netapi32")]
  306. protected static extern int NetApiBufferFree(IntPtr lpBuffer);
  307. #endregion
  308. #region Enumerate shares
  309. /// <summary>
  310. /// Enumerates the shares on Windows NT
  311. /// </summary>
  312. /// <param name="server">The server name</param>
  313. /// <param name="shares">The ShareCollection</param>
  314. protected static void EnumerateSharesNT(string server, ShareCollection shares)
  315. {
  316. int level = 2;
  317. int entriesRead, totalEntries, nRet, hResume = 0;
  318. IntPtr pBuffer = IntPtr.Zero;
  319. try
  320. {
  321. nRet = NetShareEnum(server, level, out pBuffer, -1,
  322. out entriesRead, out totalEntries, ref hResume);
  323. if (ERROR_ACCESS_DENIED == nRet)
  324. {
  325. //Need admin for level 2, drop to level 1
  326. level = 1;
  327. nRet = NetShareEnum(server, level, out pBuffer, -1,
  328. out entriesRead, out totalEntries, ref hResume);
  329. }
  330. if (NO_ERROR == nRet && entriesRead > 0)
  331. {
  332. Type t = (2 == level) ? typeof(SHARE_INFO_2) : typeof(SHARE_INFO_1);
  333. int offset = Marshal.SizeOf(t);
  334. for (int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += offset)
  335. {
  336. IntPtr pItem = new IntPtr(lpItem);
  337. if (1 == level)
  338. {
  339. SHARE_INFO_1 si = (SHARE_INFO_1)Marshal.PtrToStructure(pItem, t);
  340. shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark);
  341. }
  342. else
  343. {
  344. SHARE_INFO_2 si = (SHARE_INFO_2)Marshal.PtrToStructure(pItem, t);
  345. shares.Add(si.NetName, si.Path, si.ShareType, si.Remark);
  346. }
  347. }
  348. }
  349. }
  350. finally
  351. {
  352. // Clean up buffer allocated by system
  353. if (IntPtr.Zero != pBuffer)
  354. NetApiBufferFree(pBuffer);
  355. }
  356. }
  357. /// <summary>
  358. /// Enumerates the shares on Windows 9x
  359. /// </summary>
  360. /// <param name="server">The server name</param>
  361. /// <param name="shares">The ShareCollection</param>
  362. protected static void EnumerateShares9x(string server, ShareCollection shares)
  363. {
  364. int level = 50;
  365. int nRet = 0;
  366. ushort entriesRead, totalEntries;
  367. var t = typeof(SHARE_INFO_50);
  368. var size = Marshal.SizeOf(t);
  369. var cbBuffer = (ushort)(MAX_SI50_ENTRIES * size);
  370. //On Win9x, must allocate buffer before calling API
  371. IntPtr pBuffer = Marshal.AllocHGlobal(cbBuffer);
  372. try
  373. {
  374. nRet = NetShareEnum(server, level, pBuffer, cbBuffer,
  375. out entriesRead, out totalEntries);
  376. if (ERROR_WRONG_LEVEL == nRet)
  377. {
  378. level = 1;
  379. t = typeof(SHARE_INFO_1_9x);
  380. size = Marshal.SizeOf(t);
  381. nRet = NetShareEnum(server, level, pBuffer, cbBuffer,
  382. out entriesRead, out totalEntries);
  383. }
  384. if (NO_ERROR == nRet || ERROR_MORE_DATA == nRet)
  385. {
  386. for (int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += size)
  387. {
  388. var pItem = new IntPtr(lpItem);
  389. if (1 == level)
  390. {
  391. var si = (SHARE_INFO_1_9x)Marshal.PtrToStructure(pItem, t);
  392. shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark);
  393. }
  394. else
  395. {
  396. var si = (SHARE_INFO_50)Marshal.PtrToStructure(pItem, t);
  397. shares.Add(si.NetName, si.Path, si.ShareType, si.Remark);
  398. }
  399. }
  400. }
  401. else
  402. Console.WriteLine(nRet);
  403. }
  404. finally
  405. {
  406. //Clean up buffer
  407. Marshal.FreeHGlobal(pBuffer);
  408. }
  409. }
  410. /// <summary>
  411. /// Enumerates the shares
  412. /// </summary>
  413. /// <param name="server">The server name</param>
  414. /// <param name="shares">The ShareCollection</param>
  415. protected static void EnumerateShares(string server, ShareCollection shares)
  416. {
  417. if (null != server && 0 != server.Length && !IsW2KUp)
  418. {
  419. server = server.ToUpper();
  420. // On NT4, 9x and Me, server has to start with "\\"
  421. if (!('\\' == server[0] && '\\' == server[1]))
  422. server = @"\\" + server;
  423. }
  424. if (IsNT)
  425. EnumerateSharesNT(server, shares);
  426. else
  427. EnumerateShares9x(server, shares);
  428. }
  429. #endregion
  430. #endregion
  431. #region Static methods
  432. /// <summary>
  433. /// Returns true if fileName is a valid local file-name of the form:
  434. /// X:\, where X is a drive letter from A-Z
  435. /// </summary>
  436. /// <param name="fileName">The filename to check</param>
  437. /// <returns></returns>
  438. public static bool IsValidFilePath(string fileName)
  439. {
  440. if (null == fileName || 0 == fileName.Length) return false;
  441. char drive = char.ToUpper(fileName[0]);
  442. if ('A' > drive || drive > 'Z')
  443. return false;
  444. else if (Path.VolumeSeparatorChar != fileName[1])
  445. return false;
  446. else if (Path.DirectorySeparatorChar != fileName[2])
  447. return false;
  448. else
  449. return true;
  450. }
  451. #endregion
  452. /// <summary>The name of the server this collection represents</summary>
  453. private string _server;
  454. #region Constructor
  455. /// <summary>
  456. /// Default constructor - local machine
  457. /// </summary>
  458. public ShareCollection()
  459. {
  460. _server = string.Empty;
  461. EnumerateShares(_server, this);
  462. }
  463. /// <summary>
  464. /// Constructor
  465. /// </summary>
  466. /// <param name="server">The server.</param>
  467. public ShareCollection(string server)
  468. {
  469. _server = server;
  470. EnumerateShares(_server, this);
  471. }
  472. #endregion
  473. #region Add
  474. protected void Add(Share share)
  475. {
  476. InnerList.Add(share);
  477. }
  478. protected void Add(string netName, string path, ShareType shareType, string remark)
  479. {
  480. InnerList.Add(new Share(_server, netName, path, shareType, remark));
  481. }
  482. #endregion
  483. #region Properties
  484. /// <summary>
  485. /// Returns the name of the server this collection represents
  486. /// </summary>
  487. public string Server
  488. {
  489. get { return _server; }
  490. }
  491. /// <summary>
  492. /// Returns the <see cref="Share"/> at the specified index.
  493. /// </summary>
  494. public Share this[int index]
  495. {
  496. get { return (Share)InnerList[index]; }
  497. }
  498. /// <summary>
  499. /// Returns the <see cref="Share"/> which matches a given local path
  500. /// </summary>
  501. /// <param name="path">The path to match</param>
  502. public Share this[string path]
  503. {
  504. get
  505. {
  506. if (null == path || 0 == path.Length) return null;
  507. path = Path.GetFullPath(path);
  508. if (!IsValidFilePath(path)) return null;
  509. Share match = null;
  510. foreach (object t in InnerList)
  511. {
  512. var s = (Share)t;
  513. if (s.IsFileSystem && s.MatchesPath(path))
  514. {
  515. //Store first match
  516. if (null == match)
  517. match = s;
  518. // If this has a longer path,
  519. // and this is a disk share or match is a special share,
  520. // then this is a better match
  521. else if (match.Path.Length < s.Path.Length)
  522. {
  523. if (ShareType.Disk == s.ShareType || ShareType.Disk != match.ShareType)
  524. match = s;
  525. }
  526. }
  527. }
  528. return match;
  529. }
  530. }
  531. #endregion
  532. /// <summary>
  533. /// Copy this collection to an array
  534. /// </summary>
  535. /// <param name="array"></param>
  536. /// <param name="index"></param>
  537. public void CopyTo(Share[] array, int index)
  538. {
  539. InnerList.CopyTo(array, index);
  540. }
  541. }
  542. }