C#更改字典中的值
大家好,
Hi everyone,
我想知道是否有人可以帮我解决一段时间困扰我的问题。
I was wondering if someone could help me with an issue that has been bugging me for a while.
我有一本只有4个项目的字典。其中3个围绕着它们{{}},并且需要更改这些项目。
I have a dictionary with only 4 items in it. 3 of which have {{}} surrounding them, and these items need to be changed.
我遇到的问题是我不确定如何在一个时间内更改多个值foreach声明。
the issue I have is that I am not sure how to change multiple values at one time in a foreach statement.
我可以更改一个值,但我不确定如何重写一个文件的多个更改。
I can change one value but I am not sure how to re-write multiple changes to a file.
这里是我的代码: -
here is my code:-
public static void Diction1()
      {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP;字典<字符串,字符串> diction = new Dictionary< string,string>();
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; diction.Add("{{name}}","{{dan}}");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; diction.Add("{{work}}","{{software}}");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; diction.Add(" {{car}}"," {{honda}}");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; diction.Add("channel","2");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; string text = string.Join(Environment.NewLine,diction.Select(d => d.Key +"," + d.Value));
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; File.WriteAllText(@"C:\EDchannel2 \EDchannelTemplate.txt",text); //写入模板文件。
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; string readalltext = File.ReadAllText(@" C:\ EDchannel2 \EDchannelTemplate.txt");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; foreach(KeyValuePair< string,string> diction in diction)
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; string result = readalltext.Replace(" {{dan}}"," monkey");
$
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; string outputFile = @" C:\ EDchannel2 \EDchannel1.txt";
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; File.WriteAllText(outputFile,result); //将结果写入文件。
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b &NBSP; &NBSP; &NBSP; }
public static void Diction1()
{
Dictionary<string, string> diction = new Dictionary<string, string>();
diction.Add("{{name}}", "{{dan}}");
diction.Add("{{work}}", "{{software}}");
diction.Add("{{car}}", "{{honda}}");
diction.Add("channel", "2");
string text = string.Join(Environment.NewLine, diction.Select(d => d.Key + "," + d.Value));
File.WriteAllText(@"C:\EDchannel2\EDchannelTemplate.txt", text);// Writes to a template file.
string readalltext = File.ReadAllText(@"C:\EDchannel2\EDchannelTemplate.txt");
foreach (KeyValuePair<string, string> pair in diction)
{
string result = readalltext.Replace("{{dan}}", "monkey");
string outputFile = @"C:\EDchannel2\EDchannel1.txt";
File.WriteAllText(outputFile, result); //Writes results to a file.
}
}
如果有人有任何想法,请提供帮助。
If anyone has any ideas, please help.
谢谢。
问候
Dan
您好,
全部取决于对词典类的更改和输出文件编码。
All depends on changes to the Dictionary Class and the output file coding.
如果没有任何变化,那么您可以将 ReadAllLines()方法转换为 String [] 数组。
If nothing changes then you can method ReadAllLines() into String[] array.
然后执行搜索/替换内存,以便您可以将新数据写入
Then do a search/replace in memory so you can write the new data to the
输出文件。 如果您想使用词典类进行搜索/替换,那么
output file. If you want to search/replace using the Dictionary Class then
继续使用 ForEach 和 KeyValuePair 。在循环内部我们
IF 评估
keep using the ForEach with KeyValuePair . Inside the loop us IF to evaluate
密钥结果 True 然后更改
键的值。请记住,
the Key results to True then change the Value for that Key. Remember, you
无法从词典类添加/删除键,但可以更改
值 s
can not add/remove Keys from Dictionary Class but can change Values while
ForEach 循环执行。
希望这会有所帮助:)
Hope this helps :)