我正在尝试使用Visual Studio C#创建MDT(Microsoft部署工具)日志详细信息,并在我的ConsoleApp中为项目显示开始时间和日期。我是C#的初学者,很难编写代码。有人可以帮我提供C#代码和所需的可用库吗?
我想知道流程何时开始。
下面是创建的文件的示例。
<![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">
这是我的代码:
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();
}
}
}
如何仅从文本文件中读取第一行,并从日志文件中提取日期和时间?
答案 0 :(得分:1)
Ross为您提供了一个很好的答案,如何解析日期和时间,因此,这里是仅读取一行(如果是长文件的话)的方法。只需致电ReadLine。
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();
}
}
}
答案 1 :(得分:1)
这个问题有两个部分。我会同时解决的。
这里最简单的方法就是这样做:
var line1 = File.ReadLines(@"c:\1.txt").First(); // gets the first line from file.
请注意,这将延迟加载文件。
按照@Ross的建议,此处的首选方法是使用XML reader
或regular expression
。但是,如果时间始终是相同长度且在字符串中的相同位置,则只需通过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);
请注意,这是一种非常快捷的方法,但可能并非最佳方法。如果时间格式更改,或者第一行中的XML更改,则很容易中断。
答案 2 :(得分:0)
我猜你想读这一行
<![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">
这次打印出来
01-16-2018 09:14:26.336+480
该行实际上是XML,因此您可以使用XMLReader进行读取,并可以访问time
和date
组件。但这似乎是您无法理解的。
因此,让我们将其与正则表达式匹配,然后从匹配的字符串中构造一个dateTime:
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));
}