在文件.ini中搜索特定单词 [英] Search specific word in file .ini
本文介绍了在文件.ini中搜索特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我有一个文件.ini,我想检查一下这个文件.ini中是否存在某些单词。例如,如果在我的文件.ini中有这个字符串2605 = kl .....
如何检查我的文件中是否存在2605?
i希望用c#
i尝试使用此代码但不起作用
Hi i have a file .ini and i want to check if some words existe in this file .ini. for exxample if in my file .ini there is this string "2605=kl ....."
how do i to check if the "2605" existe in my file ??
i want to do it with c#
i tried this code but it doesn't work
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StreamReader sr;
string[]lines={""};
string[] logFiles = Directory.GetFiles("C:\\r");
foreach (string file in logFiles)
{
using (sr = new StreamReader(file))
{
foreach (string line in lines)
{
if (line.Contains("2036") )
{
System.Console.WriteLine("trouvé");
}
}
}
}
}
}
}
请帮助
我尝试了什么:
please help
What I have tried:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StreamReader sr;
string[]lines={""};
string[] logFiles = Directory.GetFiles("C:\\r");
foreach (string file in logFiles)
{
using (sr = new StreamReader(file))
{
foreach (string line in lines)
{
if (line.Contains("2036") )
{
System.Console.WriteLine("trouvé");
}
}
}
}
}
}
}
推荐答案
简单方法:
The simple way:
string s = File.ReadAllText(pathToINIFile);
if (s.Contains("2036="))
{
// Found it...
试试这个:
Try this:
string value = File.ReadAllText(@"C:\file.txt");
if (value.Contains("2036"))
{
System.Console.WriteLine("trouvé");
}
或者,一个版本不要求你一次将整个文件读入内存:
Alternatively, a version that doesn't require you to read the whole file into memory at once:
foreach (string line in File.ReadLines(pathToFile))
{
if (line.Contains("2036"))
{
Console.WriteLine("trouvé");
break; // No need to keep searching
}
}
File.ReadLines Method(String) (System.IO) [ ^ ]
这篇关于在文件.ini中搜索特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文