Record Processing
Copy codeThe Code is as follows:
Using System;
Using System. IO;
/// <Summary>
/// File
/// </Summary>
Public class File
{
Protected string FilePath;
/// <Summary>
/// File structure
/// </Summary>
/// <Param name = "filePath"> text path to be operated </param>
Public File (string filePath)
{
This. FilePath = filePath;
}
/// <Summary>
/// Write text content
/// </Summary>
/// <Param name = "info"> write content </param>
Public void FileWrite (string info)
{
Try
{
FileInfo file = new FileInfo (FilePath );
If (! File. Exists)
{
Using (StreamWriter sw = file. CreateText ())
{
Sw. WriteLine (info );
}
}
Else
{
Using (StreamWriter sw = file. AppendText ())
{
Sw. WriteLine (info );
}
}
}
Catch (FileNotFoundException fileCe)
{
Throw fileCe;
}
Catch (Exception ce)
{
Throw ce;
}
}
}
Page call code
Copy codeThe Code is as follows:
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
// Determine whether the current user has accessed the website and only records the users that have not accessed the website.
If (Request. Cookies ["IsExitsIP"] = null)
{
// Upload the TXT file every day
String fileName = string. format ("{0} {1} {2}", DateTime. now. year. toString (), DateTime. now. month. toString (), DateTime. now. day. toString ());
File file = new File (Server. MapPath ("~ /Test/"+ fileName +". txt "));
File. FileWrite (Request. UserHostName );
// Add an access tag to the user being accessed
HttpCookie cokie = new HttpCookie ("IsExitsIP ");
Cokie. Values. Add ("ip", Request. UserHostName );
Response. AppendCookie (cokie );
}
}
}
}