Home > Net >  With c # written to a file, implement the following requirements.
With c # written to a file, implement the following requirements.

Time:10-17

1. User instantiate the path of the input file, the program based on the current (date) (month) (year) to generate the corresponding file name
2. Through the thread, every two seconds to write a line of the file information, content is increasing (date) (month) (year) minutes + | + serial number
3. The realization method of the thread on off

CodePudding user response:

Which one will not yo

CodePudding user response:

Static main (string [] args)
{
A thread () {
Written to the file ()
}
}

CodePudding user response:

reference 1st floor exception92 response:
which one won't yo
second,

CodePudding user response:

StreamWriter sw=File. AppendText (logPath);
Sw. WriteLine (strLog);

CodePudding user response:

Written information=DateTime. Now. ToString (" MM - dd yyyy - HH: MM: ss ") + | + increment the serial number

CodePudding user response:

 
The static void Main (string [] args)
{

String path=Console. ReadLine ();
String STR="/" path + + DateTime. Now. The ToString (" yyyyMMdd ") + ". TXT ".
FileStream stream=File. The Create (STR);
Stream. The Close ();
TimerTask timer=new TimerTask ();
The timer. The Init (STR);
The timer. The Start ();
Console.ReadLine();

}


 

The class TimerTask
{
private Timer timer;


Private int I=0;

Private string filename="";

Public TimerTask ()
{
The timer=new timer (1000 * 1);
}

Public void Init (string path)
{
Filename=path;
The timer. Elapsed +=new ElapsedEventHandler (timer_Elapsed);
}

Void timer_Elapsed (object sender, ElapsedEventArgs e)
{

Try
{

The timer. Elapsed -=new ElapsedEventHandler (timer_Elapsed);
WriteFile ();

}
Catch Exception (err)
{
Console. WriteLine (err. Message);
}
The finally
{
The timer. Elapsed +=new ElapsedEventHandler (timer_Elapsed);
}
}

Public void WriteFile ()
{
String STR=DateTime. Now. ToString (" yyyyMMddHHmmss ") + "|" + I;
i++;
Using (System. IO StreamWriter file=new System. IO. StreamWriter (filename, true))
{
File. WriteLine (STR);
}

}

Public void the Start ()
{

The timer. The Start ();
}

Public void the Stop ()
{

The timer. Stop ();

}
}
  •  Tags:  
  • C#
  • Related