String folderpath = "D:" + "//logs";
Determine if the file exists or create it;
if (directory.exists (folderpath) = = False)
{
Directory.CreateDirectory (FolderPath);
}
String year = DateTime.Now.Year.ToString (). Trim ();
String month = DateTime.Now.Month.ToString (). Trim ();
String day = DateTime.Now.Day.ToString (). Trim ();
String filepath= folderpath+ "//log" +year + "-" + month + "-" + Day + ". txt";
if (file.exists (FilePath) = = False)
{
Create a file
FileStream fs123 = file.create (FilePath);
Fs123. Close ();
}
if (file.exists (FilePath) = = True)
//{
Clear the contents of the TXT text
FileStream stream2 = File.Open (FilePath, FileMode.OpenOrCreate, FileAccess.Write);
Stream2. Seek (0, Seekorigin.begin);
Stream2. SetLength (0); Empty TXT file
Stream2. Close ();
//}
msg = DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss.fff") + ":" + MSG + "\ r \ n";
Re-record TXT content
FileStream fs = new FileStream (FilePath, filemode.append);
Get byte array
byte[] data = System.Text.Encoding.Default.GetBytes (MSG);
Start writing
Fs. Write (data, 0, data. Length);
Empties the buffer, closes the stream
Fs. Flush ();
Fs. Close ();
File stream read/write