MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#nullable enable5 6using System;7using System.IO;8using System.Management.Automation;9using System.Runtime.InteropServices;10 11using Microsoft.Win32.SafeHandles;12 13internal static partial class Interop14{15 internal static unsafe partial class Windows16 {17 // dwDesiredAccess of CreateFile18 [Flags]19 internal enum FileDesiredAccess : uint20 {21 GenericZero = 0,22 GenericRead = 0x80000000,23 GenericWrite = 0x40000000,24 GenericExecute = 0x20000000,25 GenericAll = 0x10000000,26 }27 28 // dwFlagsAndAttributes29 [Flags]30 internal enum FileAttributes : uint31 {32 Readonly = 0x00000001,33 Hidden = 0x00000002,34 System = 0x00000004,35 Archive = 0x00000020,36 Encrypted = 0x00004000,37 Write_Through = 0x80000000,38 Overlapped = 0x40000000,39 NoBuffering = 0x20000000,40 RandomAccess = 0x10000000,41 SequentialScan = 0x08000000,42 DeleteOnClose = 0x04000000,43 BackupSemantics = 0x02000000,44 PosixSemantics = 0x01000000,45 OpenReparsePoint = 0x00200000,46 OpenNoRecall = 0x00100000,47 SessionAware = 0x00800000,48 Normal = 0x0000008049 }50 51 // WARNING: This method does not implicitly handle long paths. Use CreateFile.52 [LibraryImport("api-ms-win-core-file-l1-1-0.dll", EntryPoint = "CreateFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]53 private static unsafe partial SafeFileHandle CreateFilePrivate(54 string lpFileName,55 uint dwDesiredAccess,56 FileShare dwShareMode,57 nint lpSecurityAttributes,58 FileMode dwCreationDisposition,59 FileAttributes dwFlagsAndAttributes,60 IntPtr hTemplateFile);61 62 [LibraryImport("api-ms-win-core-file-l1-1-0.dll", EntryPoint = "CreateFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]63 private static unsafe partial nint CreateFileWithPipeHandlePrivate(64 string lpFileName,65 uint dwDesiredAccess,66 FileShare dwShareMode,67 nint lpSecurityAttributes,68 FileMode dwCreationDisposition,69 FileAttributes dwFlagsAndAttributes,70 IntPtr hTemplateFile);71 72 internal static unsafe SafeFileHandle CreateFileWithSafeFileHandle(73 string lpFileName,74 FileAccess dwDesiredAccess,75 FileShare dwShareMode,76 FileMode dwCreationDisposition,77 FileAttributes dwFlagsAndAttributes)78 {79 lpFileName = Path.TrimEndingDirectorySeparator(lpFileName);80 lpFileName = PathUtils.EnsureExtendedPrefixIfNeeded(lpFileName);81 82 return CreateFilePrivate(lpFileName, (uint)dwDesiredAccess, dwShareMode, nint.Zero, dwCreationDisposition, dwFlagsAndAttributes, nint.Zero);83 }84 85 internal static unsafe nint CreateFileWithPipeHandle(86 string lpFileName,87 FileAccess dwDesiredAccess,88 FileShare dwShareMode,89 FileMode dwCreationDisposition,90 FileAttributes dwFlagsAndAttributes)91 {92 return CreateFileWithPipeHandlePrivate(lpFileName, (uint)dwDesiredAccess, dwShareMode, nint.Zero, dwCreationDisposition, dwFlagsAndAttributes, nint.Zero);93 }94 }95}96 