المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Log file in C#



C# Programming
03-28-2009, 09:34 AM
Hi
I have listed and then started all the exe files.As and when the exe starts a log file should be maintained which will contain the start time of the exe.So i used the following code.It gives" NullException Object reference not set to an instance of an object" for the following line stw1.WriteLine("UdpServer at port 10000 has started at:", start1.ToString());
if (cb.Checked)
{
n1 = lb4.Text;
// System.Diagnostics.Process.Start(n1);
si1 = new ProcessStartInfo(n1);
proc = Process.Start(si1);
proclist.Add(proc);

DateTime start1 = proc.StartTime;
if (!File.Exists("startlog1.txt"))
{
stw1 = new StreamWriter("startlog1.txt");
}
else
{
File.AppendText("startlog1.txt");
}
stw1.WriteLine("UdpServer at port 10000 has started at:", start1.ToString());
stw1.WriteLine("=======================================================================");
stw1.Flush();
}

if (cb1.Checked)
{
n2 = lb5.Text;
//System.Diagnostics.Process.Start(n2);
ProcessStartInfo si2 = new ProcessStartInfo(n2);
proc = Process.Start(si2);
proclist.Add(proc);

DateTime start2 = proc.StartTime;
if (!File.Exists("startlog2.txt"))
{
stw2 = new StreamWriter("startlog2.txt");
}
else
{
File.AppendText("startlog2.txt");
}

stw2.WriteLine("UdpServer at port 10001 has started at:", start2.ToString());
stw2.WriteLine("=======================================================================");
stw2.Flush();

}