IOExceptionHelper.cs 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. /* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Reflection;
  11. using System.Text;
  12. using Utilities;
  13. namespace SMBLibrary.Server
  14. {
  15. public class IOExceptionHelper
  16. {
  17. public static ushort GetWin32ErrorCode(IOException ex)
  18. {
  19. int hResult = GetExceptionHResult(ex);
  20. // The Win32 error code is stored in the 16 first bits of the value
  21. return (ushort)(hResult & 0x0000FFFF);
  22. }
  23. public static int GetExceptionHResult(IOException ex)
  24. {
  25. PropertyInfo hResult = ex.GetType().GetProperty("HResult", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
  26. return (int)hResult.GetValue(ex, null);
  27. }
  28. }
  29. }