private SafeFileHandle GetSafeFileHandleForRedirection(string RedirectionPath, int dwCreationDisposition) { ProcessNativeMethods.SECURITY_ATTRIBUTES sECURITYATTRIBUTE = new ProcessNativeMethods.SECURITY_ATTRIBUTES(); IntPtr intPtr = ProcessNativeMethods.CreateFileW(RedirectionPath, ProcessNativeMethods.GENERIC_READ | ProcessNativeMethods.GENERIC_WRITE, ProcessNativeMethods.FILE_SHARE_WRITE | ProcessNativeMethods.FILE_SHARE_READ, sECURITYATTRIBUTE, dwCreationDisposition, ProcessNativeMethods.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); if (intPtr == IntPtr.Zero) { int lastWin32Error = Marshal.GetLastWin32Error(); Win32Exception win32Exception = new Win32Exception(lastWin32Error); string str = StringUtil.Format(ProcessResources.InvalidStartProcess, win32Exception.Message); ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.ThrowTerminatingError(errorRecord); } SafeFileHandle safeFileHandle = new SafeFileHandle(intPtr, true); return(safeFileHandle); }