public static void write() { StreamWriter sw = new StreamWriter(@"D:\Practice1\sample\Text8.txt"); if (sw.Equals(StreamWriter.Null)) { Console.WriteLine("Itt is null file"); } sw.WriteLine("Hello world"); sw.Close(); }
/// <summary> /// ログ出力 /// </summary> /// <param name="str">出力文字列</param> /// <param name="log_category">ログ種別</param> /// <param name="replace_category">差し替えログ種別</param> private void write_log(string str, string log_category) { if ((str == null) || (str.Equals(""))) { return; } if (_stream.Equals(null)) { Console.WriteLine(str); // ログ出力オブジェクトがない場合はコンソールに表示 return; } try { FileInfo file_info = new FileInfo(_file_name); if ((_max_output_size != 0) && (file_info.Length > _max_output_size)) { _stream.Dispose(); _fl_stream.Dispose(); _file_name = $"{_original_filename}_{_division_count}{_original_extension}"; _fl_stream = new FileStream(_file_name, FileMode.Append, FileAccess.Write, FileShare.Read); _stream = new StreamWriter(_fl_stream, Encoding.GetEncoding(_file_encode)); _division_count++; } DateTime dt_obj = DateTime.Now; string write_str = dt_obj.ToString("HH:mm:ss.fff\t"); write_str += (!log_category.Equals("")) ? $"[{log_category}]" + ":\t" : ""; write_str += str; _stream.Write(write_str); _stream.Flush(); _stream.WriteLine(); } catch (Exception e) { Console.WriteLine(e.Message); } }
private void BeginWrite(string logFile) { //</snippet3> //<snippet4> StreamWriter sw = new StreamWriter(logFile, true); //</snippet4> //<snippet5> // Gets or sets a value indicating whether the StreamWriter // will flush its buffer to the underlying stream after every // call to StreamWriter.Write. sw.AutoFlush = true; //</snippet5> //<snippet6> if (sw.Equals(StreamWriter.Null)) { sw.WriteLine("The store can be written to, but not read from."); } //</snippet6> //<snippet7> sw.Write(char.Parse(" ")); //</snippet7> //<snippet8> string hello = "Hellow World!"; char[] buffer = hello.ToCharArray(); sw.Write(buffer); //</snippet8> //<snippet9> string helloWorld = "Hellow World!"; // writes out "low World" sw.Write(helloWorld); //</snippet9> //<snippet10> sw.WriteLine("---Begin Log Entry---"); //</snippet10> //<snippet11> // Write out the current text encoding sw.WriteLine("Encoding: {0}", sw.Encoding.ToString()); //</snippet11> //<snippet12> // Display the Format Provider sw.WriteLine("Format Provider: {0} ", sw.FormatProvider.ToString()); //</snippet12> //<snippet13> // Set the characters you would like to designate a new line sw.NewLine = "\r\n"; //</snippet13> //<snippet14> ILease obj = (ILease)sw.InitializeLifetimeService(); if (obj != null) { sw.WriteLine("Object initialized lease " + "time remaining: {0}.", obj.CurrentLeaseTime.ToString() ); } //</snippet14> //<snippet15> ILease lease = (ILease)sw.GetLifetimeService(); if (lease != null) { sw.WriteLine("Object lease time remaining: {0}.", lease.CurrentLeaseTime.ToString() ); } //</snippet15> //<snippet16> // update underlying file sw.Flush(); //</snippet16> //<snippet17> // close the file by closing the writer sw.Close(); //</snippet17> //<snippet18> }