SafeFileHandle Costruttori

Definizione

Overload

SafeFileHandle()

Crea un oggetto intorno a un SafeFileHandle handle di file.

SafeFileHandle(IntPtr, Boolean)

Inizializza una nuova istanza della classe SafeFileHandle.

SafeFileHandle()

Origine:
SafeFileHandle.Unix.cs
Origine:
SafeFileHandle.Unix.cs
Origine:
SafeFileHandle.Unix.cs

Crea un oggetto intorno a un SafeFileHandle handle di file.

C#
public SafeFileHandle ();

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.NET 6, 7, 8, 9

SafeFileHandle(IntPtr, Boolean)

Origine:
SafeFileHandle.cs
Origine:
SafeFileHandle.cs
Origine:
SafeFileHandle.cs

Inizializza una nuova istanza della classe SafeFileHandle.

C#
public SafeFileHandle (IntPtr preexistingHandle, bool ownsHandle);

Parametri

preexistingHandle
IntPtr

Oggetto IntPtr che rappresenta l'handle preesistente da usare.

ownsHandle
Boolean

true per rilasciare in modo affidabile l'handle durante la fase di finalizzazione; false per impedire il rilascio affidabile (opzione non consigliata).

Esempio

Nell'esempio seguente viene illustrato come aprire un file usando la classe e la SafeFileHandle funzione non gestita CreateFile .

C#
using System;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
using System.ComponentModel;

class SafeHandlesExample
{

    static void Main()
    {
        try
        {

            UnmanagedFileLoader loader = new UnmanagedFileLoader("example.xml");
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        Console.ReadLine();
    }
}

class UnmanagedFileLoader
{

    public const short FILE_ATTRIBUTE_NORMAL = 0x80;
    public const short INVALID_HANDLE_VALUE = -1;
    public const uint GENERIC_READ = 0x80000000;
    public const uint GENERIC_WRITE = 0x40000000;
    public const uint CREATE_NEW = 1;
    public const uint CREATE_ALWAYS = 2;
    public const uint OPEN_EXISTING = 3;

    // Use interop to call the CreateFile function.
    // For more information about CreateFile,
    // see the unmanaged MSDN reference library.
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
      uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
      uint dwFlagsAndAttributes, IntPtr hTemplateFile);

    private SafeFileHandle handleValue = null;

    public UnmanagedFileLoader(string Path)
    {
        Load(Path);
    }

    public void Load(string Path)
    {
        if (Path == null && Path.Length == 0)
        {
            throw new ArgumentNullException("Path");
        }

         // Try to open the file.
        IntPtr ptr = CreateFile(Path, GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

        handleValue = new SafeFileHandle(ptr, true);

        // If the handle is invalid,
         // get the last Win32 error
         // and throw a Win32Exception.
         if (handleValue.IsInvalid)
         {
             Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
         }
    }

    public SafeFileHandle Handle
    {
        get
        {
            // If the handle is valid,
            // return it.
            if (!handleValue.IsInvalid)
            {
                return handleValue;
            }
            else
            {
                return null;
            }
        }
    }
}

Vedi anche

Si applica a

.NET 9 e altre versioni
Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0