【问题标题】:How to check if two files are the same .NET C#?如何检查两个文件是否相同.NET C#?
【发布时间】:2023-02-10 02:17:41
【问题描述】:

如何检查一个文件的内容和输入的一些单词是否相同?我想制作某种激活系统,它将检查内容是否与输入相同。

这是我想要做的:

using System.Collections.Generic;
using System;
using System.IO;

namespace Example {
  public class Example() {
    string input = Console.ReadLine();
    if (input == File(@"/prodkey.txt") {
      //code
    }
  }
}

【问题讨论】:

  • 完全一样,比如密码?或者你的意思是像搜索一样,它正在寻找文档中某处的所有单词?作为一个句子,或以任何顺序出现的单词>
  • 比较校验和?
  • 这回答了你的问题了吗? How to compare 2 files fast using .NET?
  • 您的标题说“两个文件”,但您的问题和代码说“输入的单词”——是哪个?请编辑您的问题以添加更多信息。

标签: c# .net


【解决方案1】:

您可以使用 File.ReadAllText(filePath) 获取文本文件的内容,然后使用 == 运算符或 string.Equals 方法(提供不区分大小写的比较)来比较它们:

string input = Console.ReadLine();
string fileContent = File.ReadAllText(@"/prodkey.txt");

if (input == fileContent) 
{
  
}

// Or, for case-insensitive comparison:
if (input.Equals(fileContent, StringComparison.OrdinalIgnoreCase))
{
  
}

【讨论】:

    猜你喜欢
    • 2014-06-11
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 2012-08-23
    • 2012-10-03
    • 2023-03-13
    相关资源
    最近更新 更多