Exemplo n.º 1
0
        internal static SafeFileHandle GetFileHandle(string normalizedPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
        {
            bool append = mode == FileMode.Append;

            if (append)
            {
                mode = FileMode.OpenOrCreate;
            }
            NativeMethods.EFileAccess underlyingAccess = GetUnderlyingAccess(access);

            SafeFileHandle handle = NativeMethods.CreateFile(normalizedPath, underlyingAccess, (uint)share, IntPtr.Zero, (uint)mode, (uint)options, IntPtr.Zero);

            if (handle.IsInvalid)
            {
                Exception ex = Common.GetExceptionFromLastWin32Error();
                Debug.WriteLine(string.Format("error {0} with {1}{3}{2}", ex.Message, normalizedPath, ex.StackTrace, Environment.NewLine));
                Debug.WriteLine(string.Format("{0} {1} {2} {3}", mode, access, share, options));
                throw ex;
            }

            if (append)
            {
                NativeMethods.SetFilePointer(handle, 0, SeekOrigin.End);
            }
            return(handle);
        }
Exemplo n.º 2
0
        private static SafeFileHandle CreateFileHandle(
            string devicePath,
            NativeMethods.EFileAccess desiredAccess   = 0,           // If this parameter is zero, can query certain metadata without accessing device
            NativeMethods.EFileAttributes fileAttribs = 0            // Use overlapped IO?
            )
        {
            // Default security attributes
            var security = new NativeMethods.SECURITY_ATTRIBUTES
            {
                lpSecurityDescriptor = IntPtr.Zero,
                bInheritHandle       = true,
                nLength = Marshal.SizeOf(typeof(NativeMethods.SECURITY_ATTRIBUTES))
            };

            // Get safe handle to device
            var handle = NativeMethods.CreateFile(devicePath,
                                                  desiredAccess,
                                                  NativeMethods.EFileShare.Read | NativeMethods.EFileShare.Write,
                                                  ref security,
                                                  NativeMethods.ECreationDisposition.OpenExisting,
                                                  fileAttribs,
                                                  IntPtr.Zero);

            // Throw exception if handle isn't valid
            if (handle.IsInvalid)
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            // Return handle
            return(handle);
        }
Exemplo n.º 3
0
 public static extern SafeFileHandle CreateFile(
     string lpFileName,
     NativeMethods.EFileAccess dwDesiredAccess,
     NativeMethods.EFileShare dwShareMode,
     IntPtr lpSecurityAttributes,
     NativeMethods.ECreationDisposition dwCreationDisposition,
     NativeMethods.EFileAttributes dwFlagsAndAttributes,
     IntPtr hTemplateFile);
Exemplo n.º 4
0
        private static SafeFileHandle GetFileHandle(string normalizedPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
        {
            NativeMethods.EFileAccess underlyingAccess = GetUnderlyingAccess(access);

            SafeFileHandle handle = NativeMethods.CreateFile(normalizedPath, underlyingAccess, (uint)share, IntPtr.Zero, (uint)mode, (uint)options, IntPtr.Zero);

            if (handle.IsInvalid)
            {
                throw LongPathCommon.GetExceptionFromLastWin32Error();
            }

            return(handle);
        }
        private static SafeFileHandle OpenReparsePoint(string reparsePoint, NativeMethods.EFileAccess accessMode)
        {
            SafeFileHandle reparsePointHandle = NativeMethods.CreateFile(reparsePoint, accessMode,
                                                                         NativeMethods.EFileShare.Read | NativeMethods.EFileShare.Write | NativeMethods.EFileShare.Delete,
                                                                         IntPtr.Zero, NativeMethods.ECreationDisposition.OpenExisting,
                                                                         NativeMethods.EFileAttributes.BackupSemantics | NativeMethods.EFileAttributes.OpenReparsePoint, IntPtr.Zero);

            if (Marshal.GetLastWin32Error() != 0)
            {
                ThrowLastWin32Error("Resources.UnableToOpenReparsePoint");
            }

            return(reparsePointHandle);
        }
        /// <summary>
        /// Creates or overwrites the file at the specified path.
        /// </summary>
        /// <param name="path">The path and name of the file to create. Supports long file paths.</param>
        /// <returns>A <see cref="System.IO.Stream"/> that provides read/write access to the file specified in path.</returns>
        public override System.IO.Stream CreateFile(Path path)
        {
            if (path.IsEmpty)
            {
                throw new ArgumentNullException("path");
            }

            NativeMethods.EFileAccess          fileAccess          = NativeMethods.EFileAccess.GenericWrite | NativeMethods.EFileAccess.GenericRead;
            NativeMethods.EFileShare           fileShareMode       = NativeMethods.EFileShare.None;//exclusive
            NativeMethods.ECreationDisposition creationDisposition = NativeMethods.ECreationDisposition.CreateAlways;
            SafeFileHandle hFile = NativeMethods.CreateFile(path.WithWin32LongPathPrefix(), fileAccess, fileShareMode, IntPtr.Zero, creationDisposition, NativeMethods.EFileAttributes.Normal, IntPtr.Zero);

            if (hFile.IsInvalid)
            {
                throw CreateWin32LastErrorException("Error creating file at path '{0}'.", path);
            }
            return(new System.IO.FileStream(hFile, System.IO.FileAccess.ReadWrite));
        }
        private static SafeFileHandle OpenReparsePoint(string reparsePoint, NativeMethods.EFileAccess accessMode)
        {
            // Open handle to reparse point.
            SafeFileHandle reparsePointHandle = new SafeFileHandle(
                NativeMethods.CreateFile(reparsePoint,
                                         accessMode,
                                         NativeMethods.EFileShare.Read | NativeMethods.EFileShare.Write | NativeMethods.EFileShare.Delete,
                                         IntPtr.Zero,
                                         NativeMethods.ECreationDisposition.OpenExisting,
                                         NativeMethods.EFileAttributes.BackupSemantics | NativeMethods.EFileAttributes.OpenReparsePoint,
                                         IntPtr.Zero),
                true);

            // Reparse point opened OK?
            if (Marshal.GetLastWin32Error() != 0)
            {
                throw new Win32Exception("Unable to open reparse point.");
            }

            return(reparsePointHandle);
        }