/// <summary> /// Creates a temporary file in the system's temp folder. /// </summary> /// <param name="content"></param> /// <param name="encodeAs"></param> /// <returns></returns> public FileShim TempFile(string content, EncodeAs encodeAs = EncodeAs.UTF8) { string fPath; string errMsg; FileShim tempF = null; Debug_i("Creating temporary file..."); if (!this.TryCreateNewTempFile(out fPath, out errMsg)) { return(Error_o_(tempF, "Failed to create temporary file." + L.F + errMsg)); } Debug_o("created: " + fPath); tempF = this.File(fPath); tempF.Write(content, encodeAs); return(tempF); }
public bool Write(string content , EncodeAs encoding = EncodeAs.UTF8 , bool overwriteExisting = true , bool raiseLogEvents = true ) { if (_Found && overwriteExisting) { if (!Delete()) { return(false); } } string e; //Trace_i("Writing text to file as {0}...", encoding); bool ok; try { ok = _fs.TryWriteFile(this.Path, out e, content, encoding, overwriteExisting); } catch (Exception ex) { return(LogError("_fs.TryWriteFile", ex)); } if (!raiseLogEvents) { return(ok); } if (ok) { return(Trace_n("Written text to file.", this.Path)); } else { return(Error_n("Failed to write text to file.", e)); } }
public bool TryWriteFile(string filePath , out string errMsg , string content , EncodeAs encoding , bool overwriteExisting ) { if (!this.TryCreateDir(this.GetParentDir(filePath), out errMsg)) { return(false); } return(Try(out errMsg, () => { if (encoding != EncodeAs.UTF8) { Throw.Unsupported(encoding); } if (overwriteExisting) { var file = new FileInfo(filePath); using (var fs = file.Create()) { var byts = new UTF8Encoding(false).GetBytes(content);// param "false" : do NOT append Unicode BOM fs.Write(byts, 0, byts.Length); } } else { using (var writr = new StreamWriter(filePath, true, Encoding.UTF8)) { writr.Write(content); } } })); }
public bool TryWriteFile( string filePath , out string errMsg , string content , EncodeAs encoding , bool overwriteExisting ){ if (!this.TryCreateDir(this.GetParentDir(filePath), out errMsg)) return false; return Try(out errMsg, () => { if (encoding != EncodeAs.UTF8) Throw.Unsupported(encoding); if (overwriteExisting) { var file = new FileInfo(filePath); using (var fs = file.Create()) { var byts = new UTF8Encoding(false).GetBytes(content);// param "false" : do NOT append Unicode BOM fs.Write(byts, 0, byts.Length); } } else { using (var writr = new StreamWriter(filePath, true, Encoding.UTF8)) { writr.Write(content); } } }); }
/// <summary> /// Creates a temporary file in the system's temp folder. /// </summary> /// <param name="content"></param> /// <param name="encodeAs"></param> /// <returns></returns> public FileShim TempFile(string content, EncodeAs encodeAs = EncodeAs.UTF8) { string fPath; string errMsg; FileShim tempF = null; Debug_i("Creating temporary file..."); if (!this.TryCreateNewTempFile(out fPath, out errMsg)) return Error_o_(tempF, "Failed to create temporary file." + L.F + errMsg); Debug_o("created: " + fPath); tempF = this.File(fPath); tempF.Write(content, encodeAs); return tempF; }