The Labs \ Source Viewer \ SSCLI \ Microsoft.Win32.SafeHandles \ SafeFileHandle

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  SafeFileHandle
  18. **
  19. **
  20. ** A wrapper for file handles
  21. **
  22. **
  23. ===========================================================*/
  24. using System;
  25. using System.Security;
  26. using System.Security.Permissions;
  27. using System.Runtime.InteropServices;
  28. using System.Runtime.CompilerServices;
  29. using System.Runtime.ConstrainedExecution;
  30. using System.Runtime.Versioning;
  31. using Microsoft.Win32;
  32. namespace Microsoft.Win32.SafeHandles
  33. {
  34.    
  35.     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  36.     public sealed class SafeFileHandle : SafeHandleZeroOrMinusOneIsInvalid
  37.     {
  38.        
  39.         private SafeFileHandle() : base(true)
  40.         {
  41.         }
  42.        
  43.         public SafeFileHandle(IntPtr preexistingHandle, bool ownsHandle) : base(ownsHandle)
  44.         {
  45.             SetHandle(preexistingHandle);
  46.         }
  47.        
  48.         [ResourceExposure(ResourceScope.Machine)]
  49.         [ResourceConsumption(ResourceScope.Machine)]
  50.         protected override bool ReleaseHandle()
  51.         {
  52.             return Win32Native.CloseHandle(handle);
  53.         }
  54.     }
  55. }

Developer Fusion