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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Anuja PawarPosted Dec 20, 2011, 9:17 AM
Dharmesh SharmaPosted Dec 20, 2011, 8:21 AM
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 PawarPosted Dec 20, 2011, 7:04 AM
Dharmesh SharmaPosted Dec 20, 2011, 7:02 AM
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 PawarPosted Dec 20, 2011, 5:31 AM
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();
} }
Pravin GhadgePosted Dec 20, 2011, 4:56 AM
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();
}