NetworkShares.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. namespace MediaBrowser.ServerApplication.Networking
  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. var lpItem = pBuffer.ToInt64();
  335. for (int i = 0; i < entriesRead; i++, lpItem += offset)
  336. {
  337. IntPtr pItem = new IntPtr(lpItem);
  338. if (1 == level)
  339. {
  340. SHARE_INFO_1 si = (SHARE_INFO_1)Marshal.PtrToStructure(pItem, t);
  341. shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark);
  342. }
  343. else
  344. {
  345. SHARE_INFO_2 si = (SHARE_INFO_2)Marshal.PtrToStructure(pItem, t);
  346. shares.Add(si.NetName, si.Path, si.ShareType, si.Remark);
  347. }
  348. }
  349. }
  350. }
  351. finally
  352. {
  353. // Clean up buffer allocated by system
  354. if (IntPtr.Zero != pBuffer)
  355. NetApiBufferFree(pBuffer);
  356. }
  357. }
  358. /// <summary>
  359. /// Enumerates the shares on Windows 9x
  360. /// </summary>
  361. /// <param name="server">The server name</param>
  362. /// <param name="shares">The ShareCollection</param>
  363. protected static void EnumerateShares9x(string server, ShareCollection shares)
  364. {
  365. int level = 50;
  366. int nRet = 0;
  367. ushort entriesRead, totalEntries;
  368. var t = typeof(SHARE_INFO_50);
  369. var size = Marshal.SizeOf(t);
  370. var cbBuffer = (ushort)(MAX_SI50_ENTRIES * size);
  371. //On Win9x, must allocate buffer before calling API
  372. IntPtr pBuffer = Marshal.AllocHGlobal(cbBuffer);
  373. try
  374. {
  375. nRet = NetShareEnum(server, level, pBuffer, cbBuffer,
  376. out entriesRead, out totalEntries);
  377. if (ERROR_WRONG_LEVEL == nRet)
  378. {
  379. level = 1;
  380. t = typeof(SHARE_INFO_1_9x);
  381. size = Marshal.SizeOf(t);
  382. nRet = NetShareEnum(server, level, pBuffer, cbBuffer,
  383. out entriesRead, out totalEntries);
  384. }
  385. if (NO_ERROR == nRet || ERROR_MORE_DATA == nRet)
  386. {
  387. for (int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += size)
  388. {
  389. var pItem = new IntPtr(lpItem);
  390. if (1 == level)
  391. {
  392. var si = (SHARE_INFO_1_9x)Marshal.PtrToStructure(pItem, t);
  393. shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark);
  394. }
  395. else
  396. {
  397. var si = (SHARE_INFO_50)Marshal.PtrToStructure(pItem, t);
  398. shares.Add(si.NetName, si.Path, si.ShareType, si.Remark);
  399. }
  400. }
  401. }
  402. else
  403. Console.WriteLine(nRet);
  404. }
  405. finally
  406. {
  407. //Clean up buffer
  408. Marshal.FreeHGlobal(pBuffer);
  409. }
  410. }
  411. /// <summary>
  412. /// Enumerates the shares
  413. /// </summary>
  414. /// <param name="server">The server name</param>
  415. /// <param name="shares">The ShareCollection</param>
  416. protected static void EnumerateShares(string server, ShareCollection shares)
  417. {
  418. if (null != server && 0 != server.Length && !IsW2KUp)
  419. {
  420. server = server.ToUpper();
  421. // On NT4, 9x and Me, server has to start with "\\"
  422. if (!('\\' == server[0] && '\\' == server[1]))
  423. server = @"\\" + server;
  424. }
  425. if (IsNT)
  426. EnumerateSharesNT(server, shares);
  427. else
  428. EnumerateShares9x(server, shares);
  429. }
  430. #endregion
  431. #endregion
  432. #region Static methods
  433. /// <summary>
  434. /// Returns true if fileName is a valid local file-name of the form:
  435. /// X:\, where X is a drive letter from A-Z
  436. /// </summary>
  437. /// <param name="fileName">The filename to check</param>
  438. /// <returns></returns>
  439. public static bool IsValidFilePath(string fileName)
  440. {
  441. if (null == fileName || 0 == fileName.Length) return false;
  442. char drive = char.ToUpper(fileName[0]);
  443. if ('A' > drive || drive > 'Z')
  444. return false;
  445. else if (Path.VolumeSeparatorChar != fileName[1])
  446. return false;
  447. else if (Path.DirectorySeparatorChar != fileName[2])
  448. return false;
  449. else
  450. return true;
  451. }
  452. #endregion
  453. /// <summary>The name of the server this collection represents</summary>
  454. private string _server;
  455. #region Constructor
  456. /// <summary>
  457. /// Default constructor - local machine
  458. /// </summary>
  459. public ShareCollection()
  460. {
  461. _server = string.Empty;
  462. EnumerateShares(_server, this);
  463. }
  464. /// <summary>
  465. /// Constructor
  466. /// </summary>
  467. /// <param name="server">The server.</param>
  468. public ShareCollection(string server)
  469. {
  470. _server = server;
  471. EnumerateShares(_server, this);
  472. }
  473. #endregion
  474. #region Add
  475. protected void Add(Share share)
  476. {
  477. InnerList.Add(share);
  478. }
  479. protected void Add(string netName, string path, ShareType shareType, string remark)
  480. {
  481. InnerList.Add(new Share(_server, netName, path, shareType, remark));
  482. }
  483. #endregion
  484. #region Properties
  485. /// <summary>
  486. /// Returns the name of the server this collection represents
  487. /// </summary>
  488. public string Server
  489. {
  490. get { return _server; }
  491. }
  492. /// <summary>
  493. /// Returns the <see cref="Share"/> at the specified index.
  494. /// </summary>
  495. public Share this[int index]
  496. {
  497. get { return (Share)InnerList[index]; }
  498. }
  499. /// <summary>
  500. /// Returns the <see cref="Share"/> which matches a given local path
  501. /// </summary>
  502. /// <param name="path">The path to match</param>
  503. public Share this[string path]
  504. {
  505. get
  506. {
  507. if (null == path || 0 == path.Length) return null;
  508. path = Path.GetFullPath(path);
  509. if (!IsValidFilePath(path)) return null;
  510. Share match = null;
  511. foreach (object t in InnerList)
  512. {
  513. var s = (Share)t;
  514. if (s.IsFileSystem && s.MatchesPath(path))
  515. {
  516. //Store first match
  517. if (null == match)
  518. match = s;
  519. // If this has a longer path,
  520. // and this is a disk share or match is a special share,
  521. // then this is a better match
  522. else if (match.Path.Length < s.Path.Length)
  523. {
  524. if (ShareType.Disk == s.ShareType || ShareType.Disk != match.ShareType)
  525. match = s;
  526. }
  527. }
  528. }
  529. return match;
  530. }
  531. }
  532. #endregion
  533. /// <summary>
  534. /// Copy this collection to an array
  535. /// </summary>
  536. /// <param name="array"></param>
  537. /// <param name="index"></param>
  538. public void CopyTo(Share[] array, int index)
  539. {
  540. InnerList.CopyTo(array, index);
  541. }
  542. }
  543. }