C# 如何将两个文本文件合并为一个文本文件?

C# 如何将两个文本文件合并为一个文本文件?,c#,.net,C#,.net,第一个文本文件如下所示: eu alps nl de sp fr gr it pl scan Europe Alps Benelux Germany Spain & Portugal France Greece Italy Poland Scandinavia 第二个文本文件如下所示: eu alps nl de sp fr gr it pl scan Europe Alps Benelux Germany Spain & Portugal France Greece It

第一个文本文件如下所示:

eu
alps
nl
de
sp
fr
gr
it
pl
scan
Europe
Alps
Benelux
Germany
Spain & Portugal
France
Greece
Italy
Poland
Scandinavia
第二个文本文件如下所示:

eu
alps
nl
de
sp
fr
gr
it
pl
scan
Europe
Alps
Benelux
Germany
Spain & Portugal
France
Greece
Italy
Poland
Scandinavia
我想读取这两个文本文件,并以以下格式创建一个新的文本文件:

Code = eu
Country = Europe
Code = alps
Country = Alps
Code = nl
Country = Benelux
以此类推,其余部分采用这种格式

到目前为止,我所做的是读取每个文件中的行:但不确定如何继续

IEnumerable f1 = File.ReadLines(@"c:\temp\txt1");
IEnumerable f2 = File.ReadLines(@"c:\temp\txt2");
我会这样做:

string[] f1 = File.ReadAllLines(@"c:\temp\txt1");
string[] f2 = File.ReadAllLines(@"c:\temp\txt2");
string[] f3 = new string[Math.Min(f1.Length, f2.Length)];

for (int i = 0; i < f3.Length; i++)
     f3[i] = "Code = " + f1[i] + "\nCountry = " + f2[i] +"\n";
File.WriteAllLines(@"c:\temp\txt3", f3);
string[]f1=File.ReadAllLines(@“c:\temp\txt1”);
字符串[]f2=File.ReadAllLines(@“c:\temp\txt2”);
string[]f3=新字符串[Math.Min(f1.Length,f2.Length)];
对于(int i=0;i
我会这样做:

string[] f1 = File.ReadAllLines(@"c:\temp\txt1");
string[] f2 = File.ReadAllLines(@"c:\temp\txt2");
string[] f3 = new string[Math.Min(f1.Length, f2.Length)];

for (int i = 0; i < f3.Length; i++)
     f3[i] = "Code = " + f1[i] + "\nCountry = " + f2[i] +"\n";
File.WriteAllLines(@"c:\temp\txt3", f3);
string[]f1=File.ReadAllLines(@“c:\temp\txt1”);
字符串[]f2=File.ReadAllLines(@“c:\temp\txt2”);
string[]f3=新字符串[Math.Min(f1.Length,f2.Length)];
对于(int i=0;i
压缩两个文件的对应行,选择代码和国家/地区字符串并展平字符串数组,然后将其写入文件:

File.WriteAllLines(@"c:\temp\txt3",
   f1.Zip(f2, (l1,l2) => new[] { $"Code = {l1}", $"Country = {l2}" }).SelectMany(a => a));
不要忘记为
f1
f2
使用
IEnumerable
类型。您还可以生成单个字符串:

File.WriteAllLines(@"c:\temp\txt3",
   f1.Zip(f2, (l1,l2) => $"Code = {l1}{Environment.NewLine}Country = {l2}" ));
输出:

Code = eu
Country = Europe
Code = alps
Country = Alps
Code = nl
Country = Benelux
Code = de
Country = Germany
Code = sp
Country = Spain & Portugal
Code = fr
Country = France
Code = gr
Country = Greece
Code = it
Country = Italy
Code = pl
Country = Poland
Code = scan
Country = Scandinavia

压缩两个文件的对应行,选择代码和国家/地区字符串并展平字符串数组,然后将其写入文件:

File.WriteAllLines(@"c:\temp\txt3",
   f1.Zip(f2, (l1,l2) => new[] { $"Code = {l1}", $"Country = {l2}" }).SelectMany(a => a));
不要忘记为
f1
f2
使用
IEnumerable
类型。您还可以生成单个字符串:

File.WriteAllLines(@"c:\temp\txt3",
   f1.Zip(f2, (l1,l2) => $"Code = {l1}{Environment.NewLine}Country = {l2}" ));
输出:

Code = eu
Country = Europe
Code = alps
Country = Alps
Code = nl
Country = Benelux
Code = de
Country = Germany
Code = sp
Country = Spain & Portugal
Code = fr
Country = France
Code = gr
Country = Greece
Code = it
Country = Italy
Code = pl
Country = Poland
Code = scan
Country = Scandinavia
试试这个:

List<string> codeList = File.ReadLines(@"c:\temp\txt1").ToList();
List<string> nameList = File.ReadLines(@"c:\temp\txt2").ToList();

StringBuilder codeNameBuilder = new StringBuilder();

for (int i = 0; i < codeList.Count; i++)
{
    codeNameBuilder.AppendFormat("Code = {0} \n Country = {1}", codeList[i], nameList[i]);
}

System.IO.File.WriteAllText(@"c:\temp\txtOutput.txt", codeNameBuilder.ToString());
        string CodePath = Environment.CurrentDirectory + @"\Code.txt";
        List<string> Codes = File.ReadLines(CodePath).ToList();

        string CountryPath = Environment.CurrentDirectory + @"\Country.txt";
        List<string> Countries = File.ReadLines(CountryPath).ToList();

        string result = string.Empty;
        int length = Math.Min(Codes.Count, Countries.Count);
        for (int i = 0; i < length; i++)
        {
            result += "Code = " + Codes[i] + Environment.NewLine + "Country = " + Countries[i] + Environment.NewLine;
        }

        string OutPath = Environment.CurrentDirectory + @"\out.txt";
        File.WriteAllText(OutPath, result);
List codeList=File.ReadLines(@“c:\temp\txt1”).ToList();
List name List=File.ReadLines(@“c:\temp\txt2”).ToList();
StringBuilder codeNameBuilder=新的StringBuilder();
for(int i=0;i
试试这个:

List<string> codeList = File.ReadLines(@"c:\temp\txt1").ToList();
List<string> nameList = File.ReadLines(@"c:\temp\txt2").ToList();

StringBuilder codeNameBuilder = new StringBuilder();

for (int i = 0; i < codeList.Count; i++)
{
    codeNameBuilder.AppendFormat("Code = {0} \n Country = {1}", codeList[i], nameList[i]);
}

System.IO.File.WriteAllText(@"c:\temp\txtOutput.txt", codeNameBuilder.ToString());
        string CodePath = Environment.CurrentDirectory + @"\Code.txt";
        List<string> Codes = File.ReadLines(CodePath).ToList();

        string CountryPath = Environment.CurrentDirectory + @"\Country.txt";
        List<string> Countries = File.ReadLines(CountryPath).ToList();

        string result = string.Empty;
        int length = Math.Min(Codes.Count, Countries.Count);
        for (int i = 0; i < length; i++)
        {
            result += "Code = " + Codes[i] + Environment.NewLine + "Country = " + Countries[i] + Environment.NewLine;
        }

        string OutPath = Environment.CurrentDirectory + @"\out.txt";
        File.WriteAllText(OutPath, result);
List codeList=File.ReadLines(@“c:\temp\txt1”).ToList();
List name List=File.ReadLines(@“c:\temp\txt2”).ToList();
StringBuilder codeNameBuilder=新的StringBuilder();
for(int i=0;i
试试这个:

List<string> codeList = File.ReadLines(@"c:\temp\txt1").ToList();
List<string> nameList = File.ReadLines(@"c:\temp\txt2").ToList();

StringBuilder codeNameBuilder = new StringBuilder();

for (int i = 0; i < codeList.Count; i++)
{
    codeNameBuilder.AppendFormat("Code = {0} \n Country = {1}", codeList[i], nameList[i]);
}

System.IO.File.WriteAllText(@"c:\temp\txtOutput.txt", codeNameBuilder.ToString());
        string CodePath = Environment.CurrentDirectory + @"\Code.txt";
        List<string> Codes = File.ReadLines(CodePath).ToList();

        string CountryPath = Environment.CurrentDirectory + @"\Country.txt";
        List<string> Countries = File.ReadLines(CountryPath).ToList();

        string result = string.Empty;
        int length = Math.Min(Codes.Count, Countries.Count);
        for (int i = 0; i < length; i++)
        {
            result += "Code = " + Codes[i] + Environment.NewLine + "Country = " + Countries[i] + Environment.NewLine;
        }

        string OutPath = Environment.CurrentDirectory + @"\out.txt";
        File.WriteAllText(OutPath, result);
string CodePath=Environment.CurrentDirectory+@“\code.txt”;
列表代码=File.ReadLines(CodePath.ToList();
字符串CountryPath=Environment.CurrentDirectory+@“\Country.txt”;
List Countries=File.ReadLines(CountryPath.ToList();
字符串结果=string.Empty;
int length=Math.Min(code.Count,country.Count);
for(int i=0;i
试试这个:

List<string> codeList = File.ReadLines(@"c:\temp\txt1").ToList();
List<string> nameList = File.ReadLines(@"c:\temp\txt2").ToList();

StringBuilder codeNameBuilder = new StringBuilder();

for (int i = 0; i < codeList.Count; i++)
{
    codeNameBuilder.AppendFormat("Code = {0} \n Country = {1}", codeList[i], nameList[i]);
}

System.IO.File.WriteAllText(@"c:\temp\txtOutput.txt", codeNameBuilder.ToString());
        string CodePath = Environment.CurrentDirectory + @"\Code.txt";
        List<string> Codes = File.ReadLines(CodePath).ToList();

        string CountryPath = Environment.CurrentDirectory + @"\Country.txt";
        List<string> Countries = File.ReadLines(CountryPath).ToList();

        string result = string.Empty;
        int length = Math.Min(Codes.Count, Countries.Count);
        for (int i = 0; i < length; i++)
        {
            result += "Code = " + Codes[i] + Environment.NewLine + "Country = " + Countries[i] + Environment.NewLine;
        }

        string OutPath = Environment.CurrentDirectory + @"\out.txt";
        File.WriteAllText(OutPath, result);
string CodePath=Environment.CurrentDirectory+@“\code.txt”;
列表代码=File.ReadLines(CodePath.ToList();
字符串CountryPath=Environment.CurrentDirectory+@“\Country.txt”;
List Countries=File.ReadLines(CountryPath.ToList();
字符串结果=string.Empty;
int length=Math.Min(code.Count,country.Count);
for(int i=0;i
方法1(3阵列)

创建两个数组,然后创建第三个数组来存储连接的字符串:

var codes = File.ReadAllLines( "Codes.txt" );
var countries = File.ReadAllLines( "Countries.txt" );

var lines = new List<string>( codes.Length );
for( int i = 0; i < codes.Length; i++ ) {
   lines.Add( $"Code = {codes[ i ]}" + Environment.NewLine + $"Country = {countries[ i ]}" );
}

File.WriteAllLines( "Result.txt",  lines);
方法1(3个阵列)

创建两个数组,然后创建第三个数组来存储连接的字符串:

var codes = File.ReadAllLines( "Codes.txt" );
var countries = File.ReadAllLines( "Countries.txt" );

var lines = new List<string>( codes.Length );
for( int i = 0; i < codes.Length; i++ ) {
   lines.Add( $"Code = {codes[ i ]}" + Environment.NewLine + $"Country = {countries[ i ]}" );
}

File.WriteAllLines( "Result.txt",  lines);

你确定输入的顺序是一样的吗?为什么这个问题被否决了?它似乎是关于主题的,写得很清楚,并且显示了作者的一些努力。@TannerSwett:我也在想同样的事情,我看到一些垃圾问题,得到6分,一些好问题被否决。简而言之,如果你在SE上有朋友,不管你写了多少垃圾,你都会被提升,如果没有,那么hello会被降低。事实上,我喜欢这个问题,所以我也投了赞成票,但有时投赞成票或反对票与质量无关,我在数学SE上也看到过同样的问题。我已经不想再问你的问题了,请尽我的一点力量来帮助我。@un lucky:如果有可能写一些不按输入顺序排序的东西,这比依赖它要好。所有的答案都缺少编程中最重要的部分,那就是为手头的上下文创建有意义的结构,仅仅读取和操作字符串和文件流不是编程,而是黑客行为。一个好的答案应该把这些值放在一个有意义的结构中。你确定输入的顺序是一样的吗?为什么这个问题被否决了?它似乎是关于主题的,写得很清楚,并且显示了作者的一些努力。@TannerSwett:我也在想同样的事情,我看到一些垃圾问题,得到6分,一些好问题被否决。简而言之,如果你在SE上有朋友,不管你写了多少垃圾,你都会被提升,如果没有,那么hello会被降低。事实上,我喜欢这个问题,所以我也投了赞成票,但有时投赞成票或反对票与质量无关,我在数学SE上也看到过同样的问题。我已经放弃了试着问你的问题,只要做我的一点帮助就好了。@un lucky:如果有可能写一些不需要的东西的话