File stream read/write

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.