C# 从.txt文件中读取特定值并将其存储在变量中

C# 从.txt文件中读取特定值并将其存储在变量中,c#,C#,我想读一个c语言的.txt文件,查找一个特定的值,然后将其存储在一个值本身中。例如,我有一个文本文件,example.txt,有几行,但我正在搜索一行,该行表示Damian:2014/12/04,然后仅将2014/12/04存储到初始化值示例DateTime storedate使用这个例子,我设法读取文件中的所有行并搜索特定的文件,但是我不知道如何存储它并进行修剪,以便只捕获日期,并且日期是可互换的,因此我尝试获取的只是后面的日期 int counter = 0; DateTime stored

我想读一个c语言的
.txt
文件,查找一个特定的值,然后将其存储在一个值本身中。例如,我有一个文本文件,
example.txt
,有几行,但我正在搜索一行,该行表示
Damian:2014/12/04
,然后仅将
2014/12/04
存储到初始化值示例
DateTime storedate使用这个例子,我设法读取文件中的所有行并搜索特定的文件,但是我不知道如何存储它并进行修剪,以便只捕获日期,并且日期是可互换的,因此我尝试获取的只是后面的日期

int counter = 0;
DateTime storedate;
string line;

StreamReader file = new StreamReader(@"c:\example.txt");
while ((line = file.ReadLine()) != null)
{
   if (line.Contains("Damian:"))
      // Im stuck as what to do next
}
file.Close();
这是抓住你要找的约会最简单的方法

这是最简单的抓取日期的方法。

您可能正在寻找:

代码更少,可能比替换+修剪更快

line.Split(“:”)[0]
获取冒号的左侧,在本例中,“Damian.”
line.Split(“:”)[1]
获取冒号右侧的内容

希望这有帮助

您可能正在寻找:

代码更少,可能比替换+修剪更快

line.Split(“:”)[0]
获取冒号的左侧,在本例中,“Damian.”
line.Split(“:”)[1]
获取冒号右侧的内容


希望这有帮助

您可以使用正则表达式获得完美匹配:

DateTime? storeDate = null;
using (var reader = new StreamReader(@"c:\example.txt"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        var m = Regex.Match(line, @"Damian:\s*(?<storedate>[0-9]{4}/[0-9]{2}/[0-9]{2})");
        if (m.Success)
        {
            storeDate = DateTime.Parse(m.Groups["storedate"].Value);
            // break;
        }
    }
}
if (storeDate.HasValue)
    Console.WriteLine("StoreDate = " + storeDate.Value);

可以使用正则表达式获得完美匹配:

DateTime? storeDate = null;
using (var reader = new StreamReader(@"c:\example.txt"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        var m = Regex.Match(line, @"Damian:\s*(?<storedate>[0-9]{4}/[0-9]{2}/[0-9]{2})");
        if (m.Success)
        {
            storeDate = DateTime.Parse(m.Groups["storedate"].Value);
            // break;
        }
    }
}
if (storeDate.HasValue)
    Console.WriteLine("StoreDate = " + storeDate.Value);


这比line.Split()@heh更慢,也更复杂,但与您的答案不同,它转换为DateTime,这是问题中指定的,似乎您不需要
Trim
DateTime.Parse
使用前导/尾随空格。这比line.Split()@heh更慢,更复杂,但与您的答案不同,它转换为DateTime,这是问题中指定的,似乎您不需要
Trim
DateTime。Parse
与前导/尾随空格一起工作。必须指出:
StoredDate
必须在
while
循环之前初始化,在
if语句之外
。另外,返回的值是一个
日期时间
。感谢您的编辑,Wouter!从现在起我将使用这种格式。我将使用TryParse,因为它不会引发类似于Parse的异常。只需指出:
StoredDate
必须在
while
循环之前初始化,在
if语句
之外。另外,返回的值是一个
日期时间
。感谢您的编辑,Wouter!从现在起,我将使用该格式。我将使用TryParse,因为它不会引发解析之类的异常。我如何让它显示dd/mm/yyyy而不是dd/mm/yyyy:00:00:00您可以使用重载的时间区域。比如
Console.WriteLine(“StoreDate=“+StoreDate.Value.ToString”(“dd/MM/yyyy”)
Ok@Wouter Huysentruit但这是否也会以该格式存储日期,因为我希望将日期(dd/mm/yyyy)存储在变量中只需将其存储在
DateTime
中,并忽略使用其值的时间部分。我实际上无法忽略它,我正在创建一个脚本,将文件移动到不同的日期文件夹,当它包含00:00:00时,我无法创建文件夹。我如何让它显示dd/mm/yyyy而不是dd/mm/yyy:00:00:00时间区域您可以使用重载。比如
Console.WriteLine(“StoreDate=“+StoreDate.Value.ToString”(“dd/MM/yyyy”)
Ok@Wouter Huysentruit但这是否也会以该格式存储日期,因为我希望将日期(dd/mm/yyyy)存储在变量中只需将其存储在
DateTime
中,并忽略使用其值的时间部分。我实际上无法忽略它,我正在创建一个脚本,将文件移动到不同的日期文件夹,当它包含00:00:00时,我无法创建文件夹
DateTime? storeDate = null;
using (var reader = new StreamReader(@"c:\example.txt"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        var m = Regex.Match(line, @"Damian:\s*(?<storedate>[0-9]{4}/[0-9]{2}/[0-9]{2})");
        if (m.Success)
        {
            storeDate = DateTime.Parse(m.Groups["storedate"].Value);
            // break;
        }
    }
}
if (storeDate.HasValue)
    Console.WriteLine("StoreDate = " + storeDate.Value);
DateTime? storeDate = null;
var m = Regex.Match(File.ReadAllText(@"c:\example.txt"), @"Damian:\s*(?<storedate>[0-9]{4}/[0-9]{2}/[0-9]{2})");
if (m.Success)
{
    storeDate = DateTime.Parse(m.Groups["storedate"].Value);
    Console.WriteLine("StoreDate = " + storeDate.Value);
}