将值从字典对象写入文本文件只写最后一个键值对
Writing values from dictionary object to text file only writing last key value pair
我不太了解 C# 或任何编程,但我想学习。我最近几天一直在网上搜索,将应该从 2 个文本文件中读取的内容放在一起并输出一个文件(格式化为 json 文件,我将 copy/paste 放入该文件)。所以我可以从两个文件中读取,创建一个字典对象并写入一个文件,但它只写最后一项。我相信我会一遍又一遍地覆盖,直到最后一部分。如何附加到文件而不是覆盖?
我的代码:
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WriteLangFile
{
class Program
{
static void Main(string[] args)
{
try
{
string path1 = @"C:\temp\langVars.txt";
string path2 = @"C:\temp\langValues.txt";
string[] readVars = File.ReadAllLines(path1);
string[] readVals = File.ReadAllLines(path2);
Dictionary<string, string> dictionaryVars = new Dictionary<string, string>();
for (int i = 0; i < readVars.Length; i++)
{
dictionaryVars.Add(readVars[i], readVals[i]);
}
string outputPath = ("C:\temp\");
string outputFileName = ("lang.txt");
string constant, value;
foreach (KeyValuePair<string, string> kvp in dictionaryVars)
{
constant = (kvp.Key);
value = (kvp.Value);
string[] lines = { ("\"LANG_\"" + constant + "\"_LANG \"" + " : { "),
("\"translations\" : {"),
("\"Lang.asp\" : {"),
("\"eng\"" + ": \"" + value + "\""),
("}"),
("}"),
("},")
};
using (StreamWriter outFile = new StreamWriter(Path.Combine(outputPath, outputFileName)))
{
foreach (var item in lines)
{
outFile.WriteLine(item.ToString());
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
}
您的问题是您 opening/closing 循环中的文件,并且您没有使用 constructor of StreamWriter
that takes the append
option。
最好的解决方案可能是将 StreamWriter
创建移动到循环之外,这样您只打开文件一次:
using (StreamWriter outFile = new StreamWriter(Path.Combine(outputPath, outputFileName)))
{
foreach (KeyValuePair<string, string> kvp in dictionaryVars)
{
constant = (kvp.Key);
value = (kvp.Value);
string[] lines = { ("\"LANG_\"" + constant + "\"_LANG \"" + " : { "),
("\"translations\" : {"),
("\"Lang.asp\" : {"),
("\"eng\"" + ": \"" + value + "\""),
("}"),
("}"),
("},")
};
foreach (var item in lines)
{
outFile.WriteLine(item.ToString());
}
}
}
为了完整起见,如果您要将 StreamWriter
创建保留在循环中,您可以像这样构造它,以便它附加而不是覆盖文件:
using (StreamWriter outFile = new StreamWriter(Path.Combine(outputPath, outputFileName), append: true))
P.S。看起来您 可能 正在尝试生成一个包含 JSON 的文件。目前您的代码没有生成有效的 JSON。如果你想生成 JSON,你应该使用序列化程序,例如 JSON.NET 而不是手动构建 JSON(即以 error-prone 方式)。
我不太了解 C# 或任何编程,但我想学习。我最近几天一直在网上搜索,将应该从 2 个文本文件中读取的内容放在一起并输出一个文件(格式化为 json 文件,我将 copy/paste 放入该文件)。所以我可以从两个文件中读取,创建一个字典对象并写入一个文件,但它只写最后一项。我相信我会一遍又一遍地覆盖,直到最后一部分。如何附加到文件而不是覆盖?
我的代码:
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WriteLangFile
{
class Program
{
static void Main(string[] args)
{
try
{
string path1 = @"C:\temp\langVars.txt";
string path2 = @"C:\temp\langValues.txt";
string[] readVars = File.ReadAllLines(path1);
string[] readVals = File.ReadAllLines(path2);
Dictionary<string, string> dictionaryVars = new Dictionary<string, string>();
for (int i = 0; i < readVars.Length; i++)
{
dictionaryVars.Add(readVars[i], readVals[i]);
}
string outputPath = ("C:\temp\");
string outputFileName = ("lang.txt");
string constant, value;
foreach (KeyValuePair<string, string> kvp in dictionaryVars)
{
constant = (kvp.Key);
value = (kvp.Value);
string[] lines = { ("\"LANG_\"" + constant + "\"_LANG \"" + " : { "),
("\"translations\" : {"),
("\"Lang.asp\" : {"),
("\"eng\"" + ": \"" + value + "\""),
("}"),
("}"),
("},")
};
using (StreamWriter outFile = new StreamWriter(Path.Combine(outputPath, outputFileName)))
{
foreach (var item in lines)
{
outFile.WriteLine(item.ToString());
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
}
您的问题是您 opening/closing 循环中的文件,并且您没有使用 constructor of StreamWriter
that takes the append
option。
最好的解决方案可能是将 StreamWriter
创建移动到循环之外,这样您只打开文件一次:
using (StreamWriter outFile = new StreamWriter(Path.Combine(outputPath, outputFileName)))
{
foreach (KeyValuePair<string, string> kvp in dictionaryVars)
{
constant = (kvp.Key);
value = (kvp.Value);
string[] lines = { ("\"LANG_\"" + constant + "\"_LANG \"" + " : { "),
("\"translations\" : {"),
("\"Lang.asp\" : {"),
("\"eng\"" + ": \"" + value + "\""),
("}"),
("}"),
("},")
};
foreach (var item in lines)
{
outFile.WriteLine(item.ToString());
}
}
}
为了完整起见,如果您要将 StreamWriter
创建保留在循环中,您可以像这样构造它,以便它附加而不是覆盖文件:
using (StreamWriter outFile = new StreamWriter(Path.Combine(outputPath, outputFileName), append: true))
P.S。看起来您 可能 正在尝试生成一个包含 JSON 的文件。目前您的代码没有生成有效的 JSON。如果你想生成 JSON,你应该使用序列化程序,例如 JSON.NET 而不是手动构建 JSON(即以 error-prone 方式)。