Reputation: 85
I'm trying to read the MDT (Microsoft Deployment Tool) log details when it is created using Visual Studio C# and display start time and date in my ConsoleApp for my project. I'm a beginner for C# and having a hard time to code. Can someone please help me with the C# code and available libraries required for the same?
I want to know when the process has started.
Below is an example of the file that is created.
<![LOG[LOGGING: Finalize process ID set to 1036]LOG]!><time="09:14:26.336+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1040" file="tslogging.cpp:1864">
<![LOG[==============================[ TSBootShell.exe ]==============================]LOG]!><time="09:14:26.336+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1040" file="bootshell.cpp:1206">
<![LOG[Succeeded loading resource DLL 'X:\sms\bin\x64\1033\TSRES.DLL']LOG]!><time="09:14:26.336+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1040" file="util.cpp:972">
<![LOG[Debug shell is enabled]LOG]!><time="09:14:26.336+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1040" file="bootshell.cpp:1217">
<![LOG[Waiting for PNP initialization...]LOG]!><time="09:14:26.351+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="bootshell.cpp:69">
<![LOG[RAM Disk Boot Path: MULTI(0)DISK(0)RDISK(0)PARTITION(1)\SOURCES\BOOT.WIM]LOG]!><time="09:14:26.351+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="configpath.cpp:322">
<![LOG[WinPE boot path: D:\SOURCES\BOOT.WIM]LOG]!><time="09:14:26.351+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="configpath.cpp:347">
<![LOG[Booted from removable device]LOG]!><time="09:14:26.351+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="configpath.cpp:377">
<![LOG[Found config path D:\]LOG]!><time="09:14:26.351+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="bootshell.cpp:656">
<![LOG[Booting from removable media, not restoring bootloaders on hard drive]LOG]!><time="09:14:26.351+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="bootshell.cpp:721">
<![LOG[D:\WinPE does not exist.]LOG]!><time="09:14:26.523+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="bootshell.cpp:738">
<![LOG[D:\_SmsTsWinPE\WinPE does not exist.]LOG]!><time="09:14:26.523+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="bootshell.cpp:752">
<![LOG[Executing command line: wpeinit.exe -winpe]LOG]!><time="09:14:26.539+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="bootshell.cpp:1011">
<![LOG[The command completed successfully.]LOG]!><time="09:14:32.378+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="bootshell.cpp:1093">
<![LOG[Setting offline Windows drive and OS root directory to TS envirtonment.]LOG]!><time="09:14:32.378+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="bootshell.cpp:806">
<![LOG[ Processing volume D:\ ]LOG]!><time="09:14:32.378+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="bootshell.cpp:549">
<![LOG[ Volume D:\ is not a local hard drive.]LOG]!><time="09:14:32.378+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1056" file="bootshell.cpp:552">
And this is my code:
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\1.txt";
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
}
How can I read only the first line from a text file and extract date and time from the log file?
Upvotes: 0
Views: 6655
Reputation: 6255
I am guessing you want to read this line
<![LOG[LOGGING: Finalize process ID set to 1036]LOG]!><time="09:14:26.336+480" date="01-16-2018" component="TSBootShell" context="" type="1" thread="1040" file="tslogging.cpp:1864">
And print out this time
01-16-2018 09:14:26.336+480
That line is actually XML, so you could read it with an XMLReader and get access to the time
and date
components. But that seems like it might be beyond your understanding.
So let's match it with a regular expression, then construct a dateTime from the matched strings:
Regex rg = new Regex(@"time=""([^""]+)"" date=""([^""]+)""");
Match m = rg.Match(s);
if (m != null)
{
string timeString = m.Groups[1].Captures[0].Value;
string dateString = m.Groups[2].Captures[0].Value;
DateTime theDate = DateTime.Parse(dateString+" "+timeString.Substring(0,12));
}
Upvotes: 0
Reputation: 17402
There's two parts to this question. I'll address both.
The easiest way here is simply to do:
var line1 = File.ReadLines(@"c:\1.txt").First(); // gets the first line from file.
Note that this will lazy load the file.
As recommended by @Ross, the preferred method here is to either use an XML reader
or a regular expression
. Though, if the time is always going to be the same length and at the same location in the string, you can simply do a lookup via IndexOf
.
var line1 = File.ReadLines("MyFile.txt").First(); // gets the first line from file.
var timeTag = "time=";
var timeLength = 16;
var startTimeIndex = a.IndexOf(timeTag, 0);
var time = a.Substring(startTimeIndex + timeTag.Length + 1, timeLength);
Note that this is a really quick way of doing it, but may not be the best. If the time format changes, or the XML in the first line changes, this can easily break.
Upvotes: 4
Reputation: 3231
Ross has give you a great answer for how to parse the date and time, so here's how to just read a single line (in case it's a long file). Just call ReadLine once.
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\1.txt";
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = sr.ReadLine();
Console.WriteLine(s);
Console.ReadLine();
}
}
}
Upvotes: 2