C# 检查文件中是否存在字符串
我有下面一段代码,打开一个文本文件,读取文件中的所有行,并将其存储到字符串数组中 然后检查数组中是否存在该字符串。 然而,我面临的问题是,每当找到字符串时,它总是显示“存在匹配项”以及“不存在匹配项”知道如何解决这个问题吗? 检查此代码:C# 检查文件中是否存在字符串,c#,string,streamreader,C#,String,Streamreader,我有下面一段代码,打开一个文本文件,读取文件中的所有行,并将其存储到字符串数组中 然后检查数组中是否存在该字符串。 然而,我面临的问题是,每当找到字符串时,它总是显示“存在匹配项”以及“不存在匹配项”知道如何解决这个问题吗? 检查此代码: using (StreamReader sr = File.OpenText(path)) { string[] lines = File.ReadAllLines(path); for (int x = 0; x < lines.Len
using (StreamReader sr = File.OpenText(path))
{
string[] lines = File.ReadAllLines(path);
for (int x = 0; x < lines.Length - 1; x++)
{
if (domain == lines[x])
{
sr.Close();
MessageBox.Show("there is a match");
}
}
if (sr != null)
{
sr.Close();
MessageBox.Show("there is no match");
}
}
使用(StreamReader sr=File.OpenText(path))
{
string[]lines=File.ReadAllLines(路径);
对于(int x=0;x
听起来太复杂了,如果您想知道文件中是否存在字符串,没有理由逐行检查。您可以简单地将所有代码替换为:
if(File.ReadAllText(path).Contains(domain))
{
MessageBox.Show("There is a match");
}
您可以尝试以下代码:
using (StreamReader sr = File.OpenText(path))
{
string[] lines = File.ReadAllLines(path);
for (int x = 0; x < lines.Length - 1; x++)
{
if (lines[x].Contains(domain, StringComparison.InvariantCultureIgnoreCase)
{
sr.Close();
MessageBox.Show("there is a match");
}
}
if (sr != null)
{
sr.Close();
MessageBox.Show("there is no match");
}
}
使用(StreamReader sr=File.OpenText(path))
{
string[]lines=File.ReadAllLines(路径);
对于(int x=0;x
我建议如下设置和标记并检查它
using (StreamReader sr = File.OpenText(path))
{
string[] lines = File.ReadAllLines(path);
bool isMatch = false;
for (int x = 0; x < lines.Length - 1; x++)
{
if (domain == lines[x])
{
sr.Close();
MessageBox.Show("there is a match");
isMatch = true;
}
}
if (!isMatch)
{
sr.Close();
MessageBox.Show("there is no match");
}
}
使用(StreamReader sr=File.OpenText(path))
{
string[]lines=File.ReadAllLines(路径);
bool-isMatch=false;
对于(int x=0;x
祝您好运!实际上您不需要将整个文件读入内存。有一种方法允许您逐个枚举文件行,而无需读取整个文件。您可以创建以下方法
private bool DomainExists(string domain)
{
foreach(string line in File.ReadLines(path))
if (domain == line)
return true; // and stop reading lines
return false;
}
此方法的用法如下所示:
if (DomainExists(domain))
MessageBox.Show("there is a match");
else
MessageBox.Show("there is no match");
还有两个旁注-如果您正在使用File.ReadAllLines
读取行,则不需要StreamReader
(它在内部创建读卡器)。只需检查-您甚至不在任何地方使用sr
变量。第二个注意-如果您使用块将流包装在中,则不需要手动关闭流。在这种情况下,流将被自动处理和关闭。最简单的方法:
string content = File.ReadAllText(path);
if (content.IndexOf(domain) > -1)
{
// domain exists
}
else
{
// domain does not exist
}
现在来分析您的代码:
首先,您正在创建StreamReader实例,但稍后不会在代码中使用它
using (StreamReader sr = File.OpenText(path)) // you can remove this line
{
string[] lines = File.ReadAllLines(path); // as you are not using it here
for (int x = 0; x < lines.Length - 1; x++)
{
if (domain == lines[x])
{
sr.Close();
MessageBox.Show("there is a match");
hasMatch = true;
break; // exit loop if found
}
}
if (!hasMatch)
{
// there is no match
}
if (sr != null) // you dont need this if you remove it from the beginning of the code
{
sr.Close();
MessageBox.Show("there is no match");
}
}
第二,如果域名在文件中多次出现怎么办?在你的代码中,你会得到多个“有匹配项”在你的代码中
using (StreamReader sr = File.OpenText(path)) // you can remove this line
{
string[] lines = File.ReadAllLines(path); // as you are not using it here
for (int x = 0; x < lines.Length - 1; x++)
{
if (domain == lines[x])
{
sr.Close();
MessageBox.Show("there is a match");
hasMatch = true;
break; // exit loop if found
}
}
if (!hasMatch)
{
// there is no match
}
if (sr != null) // you dont need this if you remove it from the beginning of the code
{
sr.Close();
MessageBox.Show("there is no match");
}
}
使用(StreamReader sr=File.OpenText(path))//您可以删除此行
{
string[]lines=File.ReadAllLines(path);//因为您没有在这里使用它
对于(int x=0;x
试一试试一试:
string x;
string log = @"C:\Users\Log.txt";
string ruta = @"C:\Users\x.txt";
if (File.Exists(ruta))
{
try
{
x = File.ReadAllText(ruta);
}
catch (Exception ex)
{
File.AppendAllText(ruta, "Something");
File.AppendAllText(log, Environment.NewLine + DateTime.Now.ToString() + ": The file not contain a string. " + ex.Message);
}
}
您可以试试这个:
using (StreamReader sr = File.OpenText(path))
{
string[] lines = File.ReadAllLines(path);
for (int x = 0; x < lines.Length - 1; x++)
{
if (domain == lines[x])
{
sr.Close();
MessageBox.Show("there is a match");
}
}
if (sr != null)
{
sr.Close();
MessageBox.Show("there is no match");
}
}
首先,您需要创建一个接收字符串类型数组的方法,然后我们将该数组转换为字符串,然后我们读取txt中的所有文本,我们可以使用(Contains)来知道我们发送的文本是否存在于txt中,我们验证这是真是假,希望这对您有所帮助
public static void TextValidation(string[] val){
//Path of your file
string path = "/Users/Desktop/YourFile.txt";
//Array to string
string b = string.Join(",",val);
//Validate if exists
if(File.ReadAllText(path).Contains(b)){
Console.WriteLine("found");
// Do something if the data is found
}else{
Console.WriteLine("Not found");
}
}
即使找到匹配项,仍将显示“没有匹配项”对话框。如果您想突出显示字符串.Contains()
的用法,我将澄清答案。请解释您的答案。欢迎使用StackOverflow!介意解释这是如何解决问题的吗?谢谢!