internal static StreamReader OpenStreamReader(PSCmdlet command, string filePath, string encoding, bool isLiteralPath) { FileStream stream = OpenFileStream(filePath, command, isLiteralPath); if (encoding == null) { return(new StreamReader(stream)); } return(new StreamReader(stream, EncodingConversion.Convert(command, encoding))); }
/// <summary> /// THE method for opening a file for writing. /// Should be used by all cmdlets that write to a file. /// </summary> /// <param name="cmdlet">Cmdlet that is opening the file (used mainly for error reporting).</param> /// <param name="filePath">Path to the file (as specified on the command line - this method will resolve the path).</param> /// <param name="encoding">Encoding (this method will convert the command line string to an Encoding instance).</param> /// <param name="defaultEncoding">If <see langword="true"/>, then we will use default .NET encoding instead of the encoding specified in <paramref name="encoding"/> parameter.</param> /// <param name="Append"></param> /// <param name="Force"></param> /// <param name="NoClobber"></param> /// <param name="fileStream">Result1: <see cref="FileStream"/> opened for writing.</param> /// <param name="streamWriter">Result2: <see cref="StreamWriter"/> (inherits from <see cref="TextWriter"/>) opened for writing.</param> /// <param name="readOnlyFileInfo">Result3: file info that should be used to restore file attributes after done with the file (<see langword="null"/> is this is not needed).</param> /// <param name="isLiteralPath">True if wildcard expansion should be bypassed.</param> internal static void MasterStreamOpen( PSCmdlet cmdlet, string filePath, string encoding, bool defaultEncoding, bool Append, bool Force, bool NoClobber, out FileStream fileStream, out StreamWriter streamWriter, out FileInfo readOnlyFileInfo, bool isLiteralPath ) { Encoding resolvedEncoding = EncodingConversion.Convert(cmdlet, encoding); MasterStreamOpen(cmdlet, filePath, resolvedEncoding, defaultEncoding, Append, Force, NoClobber, out fileStream, out streamWriter, out readOnlyFileInfo, isLiteralPath); }
internal static void MasterStreamOpen( PSCmdlet cmdlet, string filePath, string encoding, bool defaultEncoding, bool Append, bool Force, bool NoClobber, out FileStream fileStream, out StreamWriter streamWriter, out FileInfo readOnlyFileInfo) { fileStream = (FileStream)null; streamWriter = (StreamWriter)null; readOnlyFileInfo = (FileInfo)null; string str = PathUtils.ResolveFilePath(filePath, cmdlet); Encoding encoding1 = EncodingConversion.Convert((Cmdlet)cmdlet, encoding); try { FileMode mode = FileMode.Create; if (Append) { mode = FileMode.Append; } else if (NoClobber) { mode = FileMode.CreateNew; } if (Force && (Append || !NoClobber) && File.Exists(str)) { FileInfo fileInfo = new FileInfo(str); if ((fileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { readOnlyFileInfo = fileInfo; fileInfo.Attributes &= ~FileAttributes.ReadOnly; } } FileShare share = Force ? FileShare.ReadWrite : FileShare.Read; fileStream = new FileStream(str, mode, FileAccess.Write, share); if (defaultEncoding) { streamWriter = new StreamWriter((Stream)fileStream); } else { streamWriter = new StreamWriter((Stream)fileStream, encoding1); } } catch (ArgumentException ex) { PathUtils.ReportFileOpenFailure((Cmdlet)cmdlet, str, (Exception)ex); } catch (IOException ex) { if (NoClobber && File.Exists(str)) { cmdlet.ThrowTerminatingError(new ErrorRecord((Exception)ex, nameof(NoClobber), ErrorCategory.ResourceExists, (object)str) { ErrorDetails = new ErrorDetails((Cmdlet)cmdlet, nameof(PathUtils), "UtilityFileExistsNoClobber", new object[2] { (object)filePath, (object)nameof(NoClobber) }) }); } PathUtils.ReportFileOpenFailure((Cmdlet)cmdlet, str, (Exception)ex); } catch (UnauthorizedAccessException ex) { PathUtils.ReportFileOpenFailure((Cmdlet)cmdlet, str, (Exception)ex); } catch (NotSupportedException ex) { PathUtils.ReportFileOpenFailure((Cmdlet)cmdlet, str, (Exception)ex); } catch (SecurityException ex) { PathUtils.ReportFileOpenFailure((Cmdlet)cmdlet, str, (Exception)ex); } }