hello,
in my asp.net project i mainten a txt file but i want to add system time in txt file but i try many code but not find solution
can anybody give me solution to write in txt file
thanks
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pravin GhadgeRank #1,340 · 1.3K reputationPosted Dec 19, 2011, 11:56 PM EST
Try this,
public void writeFile()
{
FileStream fs = new FileStream(@"c:\Temp\Backup.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
string strWrite=Datetime.Today.Date.Tostring();
m_streamWriter.WriteLine(strWrite);
m_streamWriter.Flush();
m_streamWriter.Close();
}
Anuja PawarRank #1,404 · 1.2K reputationPosted Dec 20, 2011, 12:31 AM EST
using System;
using System.IO;
class DirAppend {
public static void Main(String[] args)
{
StreamWriter w = File.AppendText("log.txt");
Log ("Test1", w);
Log ("Test2", w); // Close the writer and underlying
file. w.Close(); // Open and read the file.
StreamReader r = File.OpenText("log.txt");
DumpLog (r);
}
public static void Log (String logMessage, TextWriter w)
{
w.Write("\r\nLog Entry : ");
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
w.WriteLine(" :");
w.WriteLine(" :{0}", logMessage);
w.WriteLine ("-------------------------------"); // Update the underlying file. w.Flush(); }
public static void DumpLog (StreamReader r)
{ // While not at the end of the file, read and write lines.
String line;
while ((line=r.ReadLine())!=null)
{ Console.WriteLine(line);
}
r.Close();
} }
Dharmesh SharmaRank #1,974 · 741 reputationPosted Dec 20, 2011, 2:02 AM EST
this is code for only Window application and provied by Microsoft
and i have already use this code
this is not working for asp.net
and i have find own solution
thanks
Anuja PawarRank #1,404 · 1.2K reputationPosted Dec 20, 2011, 2:04 AM EST
Dharmesh SharmaRank #1,974 · 741 reputationPosted Dec 20, 2011, 3:21 AM EST
StreamWriter log;
if (!File.Exists(Server.MapPath("log.txt")))
{
log = new StreamWriter(Server.MapPath("log.txt"));
}
else
{
log = File.AppendText(Server.MapPath("log.txt"));
}
// Write to the file:
log.WriteLine(DateTime.Now);
log.WriteLine(strResponse);
// Close the stream:
log.Close();
Anuja PawarRank #1,404 · 1.2K reputationPosted Dec 20, 2011, 4:17 AM EST