【发布时间】:2015-05-26 15:50:44
【问题描述】:
以下是我项目的总体要点: 我有三个文件:file1、file2 和 file3。这些文件里面是一堆电子邮件。 file1 应该包含所有电子邮件,其中 file2 有一组,file3 有另一组。我需要编写一个程序来读取这些文件,查看这 3 封电子邮件中缺少哪些电子邮件,然后写入一个名为主文件的新文件,主文件的输出必须告诉我每个电子邮件中缺少哪些电子邮件文件。
例如,假设 Anna 在 file2 中,但在 file1 中找不到 Anna。主文件的输出应该告诉我需要将 Anna 添加到 file1(我知道 Anna 不是电子邮件,但这只是一个示例。)
下面是我一直在处理的代码,以及用于查找和/或编写文本文件的代码。我的问题是如何获取这些文件并将它们放入一个数组中,以便对它们进行排序并获得我想要的输出。或者如果这不是你的处理方式,我必须如何处理。
class Program
{
const string file1 = "file1.txt";
const string file2 = "file2.txt";
const string file3 = "file3.txt";
static void Main(string[] args)
{
try
{
var count = File.ReadLines(file1).Count();
//This code simply checks if the full_pack file exists and if it does, it will give how many lines are in the file
if (!File.Exists(file1))
{
Console.WriteLine("File Not Found");
}
else
{
string[] lines = System.IO.File.ReadAllLines(file1);
Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file1));
Console.WriteLine(lines.Count());
}
//this code is similar to the one above, however it is for den1
if (!File.Exists(file2))
{
Console.WriteLine("File Not Found");
}
else
{
string[] lines = System.IO.File.ReadAllLines(file1);
Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file2));
Console.WriteLine(lines.Count());
}
//this code is similar to the one above, however it is for den2
if (!File.Exists(file3))
{
Console.WriteLine("File Not Found");
}
else
{
string[] lines = System.IO.File.ReadAllLines(file3);
Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file3));
Console.WriteLine(lines.Count());
}
//This code is to create a new file called masterfile
string masterFile = @"C:\Users\povermyer\documents\visual studio 2013\Projects\BoyscoutProject2\BoyscoutProject2\Data\masterFile.txt";
if (!File.Exists(masterFile))
{
File.Create(masterFile);
TextWriter tw = new StreamWriter(masterFile);
tw.WriteLine("Here is the list of missing emails");
tw.Close();
}
else if (File.Exists(masterFile))
{
TextWriter tw = new StreamWriter(masterFile);
tw.WriteLine("Here is the list of missing emails");
tw.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
Console.ReadKey();
}
【问题讨论】:
-
是的,用 c# 标记它通常也会给我们这些信息。
-
关于主题:您已经有了
string[] lines = System.IO.File.ReadAllLines(file1);行,它将所有电子邮件放入一个数组中,那么问题是什么? -
它将它放入数组是的,我需要知道如何从这三个文件中获取所有信息,查看这些文件中缺少哪些电子邮件,然后输出主文件阅读我需要从哪个文件添加到 file1 的电子邮件。
-
感谢所有在这个漫长的编码过程中提供帮助的人。我感谢每个人为编写此代码所做的所有辛勤工作!
标签: c#