Ejemplo n.º 1
0
        private FileStream WindowsCreateFile(string fileName, bool allowConcurrentWrite)
        {
            var fileShare = Win32FileNativeMethods.FILE_SHARE_READ;

            if (allowConcurrentWrite)
            {
                fileShare |= Win32FileNativeMethods.FILE_SHARE_WRITE;
            }

            if (CreateFileParameters.EnableFileDelete && PlatformDetector.CurrentOS != RuntimeOS.Windows)
            {
                fileShare |= Win32FileNativeMethods.FILE_SHARE_DELETE;
            }

            var handle = Win32FileNativeMethods.CreateFile(
                fileName,
                Win32FileNativeMethods.FileAccess.GenericWrite,
                fileShare,
                IntPtr.Zero,
                Win32FileNativeMethods.CreationDisposition.OpenAlways,
                CreateFileParameters.FileAttributes,
                IntPtr.Zero);

            if (handle.ToInt32() == -1)
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            var safeHandle  = new SafeFileHandle(handle, true);
            var returnValue = new FileStream(safeHandle, FileAccess.Write, CreateFileParameters.BufferSize);

            returnValue.Seek(0, SeekOrigin.End);
            return(returnValue);
        }
Ejemplo n.º 2
0
        private FileStream WindowsCreateFile(string fileName, bool allowFileSharedWriting)
        {
            int fileShare = Win32FileNativeMethods.FILE_SHARE_READ;

            if (allowFileSharedWriting)
            {
                fileShare |= Win32FileNativeMethods.FILE_SHARE_WRITE;
            }

            if (this.CreateFileParameters.EnableFileDelete && PlatformDetector.CurrentOS != RuntimeOS.Windows)
            {
                fileShare |= Win32FileNativeMethods.FILE_SHARE_DELETE;
            }

            Microsoft.Win32.SafeHandles.SafeFileHandle handle = null;
            FileStream fileStream = null;

            try
            {
                handle = Win32FileNativeMethods.CreateFile(
                    fileName,
                    Win32FileNativeMethods.FileAccess.GenericWrite,
                    fileShare,
                    IntPtr.Zero,
                    Win32FileNativeMethods.CreationDisposition.OpenAlways,
                    this.CreateFileParameters.FileAttributes,
                    IntPtr.Zero);

                if (handle.IsInvalid)
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }

                fileStream = new FileStream(handle, FileAccess.Write, this.CreateFileParameters.BufferSize);
                fileStream.Seek(0, SeekOrigin.End);
                return(fileStream);
            }
            catch
            {
                if (fileStream != null)
                {
                    fileStream.Dispose();
                }

                if ((handle != null) && (!handle.IsClosed))
                {
                    handle.Close();
                }

                throw;
            }
        }
Ejemplo n.º 3
0
        private FileStream WindowsCreateFile(string fileName, bool allowFileSharedWriting)
        {
            int fileShare = Win32FileNativeMethods.FILE_SHARE_READ;

            if (allowFileSharedWriting)
            {
                fileShare |= Win32FileNativeMethods.FILE_SHARE_WRITE;
            }

            if (this.CreateFileParameters.EnableFileDelete && PlatformDetector.CurrentOS != RuntimeOS.Windows)
            {
                fileShare |= Win32FileNativeMethods.FILE_SHARE_DELETE;
            }

            Microsoft.Win32.SafeHandles.SafeFileHandle handle = null;
            FileStream fileStream = null;

            try
            {
                handle = Win32FileNativeMethods.CreateFile(
                    fileName,
                    Win32FileNativeMethods.FileAccess.GenericWrite | Win32FileNativeMethods.FileAccess.GenericRead,
                    fileShare,
                    IntPtr.Zero,
                    Win32FileNativeMethods.CreationDisposition.OpenAlways,
                    this.CreateFileParameters.FileAttributes,
                    IntPtr.Zero);

                if (handle.IsInvalid)
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }

#if !SILVERLIGHT && !MONO && !__IOS__ && !__ANDROID__ && NET4_5
                // TODO: Beware there be dragons here, every time we create a file stream, it creates a byte array the size of the buffersize.
                // TODO: in MS .NET implementation, the file stream does write to its internal buffer if you are writing something that is bigger than its buffer size,
                // So, to prevent allocations, we set the buffer to 1, since then it will just write whats in the incoming buffer and not allocate a buffer
                if (this.CreateFileParameters.PoolingEnabled)
                {
                    fileStream = new FileStream(handle, FileAccess.ReadWrite, 1);
                }
                else
                {
                    fileStream = new FileStream(handle, FileAccess.ReadWrite, this.CreateFileParameters.BufferSize);
                }
#else
                fileStream = new FileStream(handle, FileAccess.Write, this.CreateFileParameters.BufferSize);
#endif


                fileStream.Seek(0, SeekOrigin.End);
                return(fileStream);
            }
            catch
            {
                if (fileStream != null)
                {
                    fileStream.Dispose();
                }

                if ((handle != null) && (!handle.IsClosed))
                {
                    handle.Close();
                }

                throw;
            }
        }