已添加具有相同键的字典错误项
dictionary error item with the same key has already been added
我正在尝试读取 C++ header 中的每个 #define
,并为每个值显示一个编辑器,以便我可以快速重新定义常量。
我正在尝试解析文件,将左侧的每个项目作为关键。我想在左侧显示值,编辑值并保存回来。目前,当我保存我的文件时,它弄乱了格式。
这是我目前拥有的代码:
private String header;
private Dictionary<String, String> tokens;
private String file = Properties.Settings.Default.path_location;
public TextBox elf_naparm_zombie_developer;
public harrs_gsh_editor(string file)//TextBox elf_naparm_zombie_developer
{
Properties.Settings.Default.path_location = file;
Properties.Settings.Default.Save();
loadweapon();
// this.elf_naparm_zombie_developer = elf_naparm_zombie_developer;
}
public void loadweapon()
{
this.tokens = File.ReadLines(this.file)
.Select(line => Regex.Match(line, @"^\s*#define\s+(.+?)(\s+(.+?))?((\s*//)|$)"))
.Where(m => m.Success && m.Groups[3].Success)
.ToDictionary(m => m.Groups[1].Value, m => m.Groups[3].Value);
}
public String Search(String name)
{
foreach (KeyValuePair<String, String> pair in tokens)
{
if (pair.Key == name)
return pair.Value;
}
return null;
}
public void Set(String key, String val)
{
tokens[key] = val;
}
public void Save(String file)
{
using (StreamWriter sw = new StreamWriter(File.OpenWrite(file)))
{
sw.Write(header);
foreach (KeyValuePair<String, String> pair in tokens)
{
sw.Write("\r\n" + pair.Key + "#define " + pair.Value);
}
}
}
public void Save()
{
this.Save(file);
}
我遇到的问题是当我将它保存回来时,它弄乱了文件并删除了#define。我想做的只是更改值并将其用更改后的值写回程序
这是file i want to edit
所以我想做的是加载选定的键并显示值 我已经设法用我创建的这段代码来做,但我遇到的问题是把它写回去
我用来加载和填充文本框的代码
namespace Harry_s_Template_Editor
{
public partial class Form1 : Form
{
harrs_gsh_editor elfghc;
string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"/hb21_zm_ai_napalm.gsh";
public Form1()
{
InitializeComponent();
elfghc = new harrs_gsh_editor(path);//TextBox elf_naparm_zombie_developer
elf_naparm_zombie_developer = elf_naparm_zombie_developer;
// MessageBox.Show(path);
}
private void button1_Click(object sender, EventArgs e)
{
elfghc.loadweapon();
this.elf_naparm_zombie_developer.Text = elfghc.Search("NAPALM_ZOMBIE_DEVELOPER_DEBUG_PRINTS");
elfghc.Set("NAPALM_ZOMBIE_DEVELOPER_DEBUG_PRINTS", elf_naparm_zombie_developer.Text);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
elfghc.Set("NAPALM_ZOMBIE_DEVELOPER_DEBUG_PRINTS", elf_naparm_zombie_developer.Text);
elfghc.Save(path);
}
}
}
当我保存文件时,当我尝试再次加载程序时出现此错误
error
在此先感谢 Yuki
原回答:
为什么要按以下顺序将其写回文件:sw.Write("\r\n" + pair.Key + "#define " + pair.Value);
? #define
不应该排在 \r\n
之后吗?我原以为应该是这样的:sw.Write("\r\n#define " + pair.Key + " " + pair.Value);
新答案(问题已更改)
你的操作有两个问题:
您正在将 key/value 对写回文件,忽略原始文件中的 headers 和制作者名单。如果您乐于将它们剥离,那很好,但听起来这对您很重要。因此,您需要读取文件,找到文件中的键,然后更新值并将更新后的文件保存回磁盘。这比简单地将一堆 key/value 对写入文件更难。
第二个问题是您将文件写回磁盘的方式。根据 this MSDN 页面,File.OpenWrite
具有以下行为:
The OpenWrite method opens a file if one already exists for the file
path, or creates a new file if one does not exist. For an existing
file, it does not append the new text to the existing text. Instead,
it overwrites the existing characters with the new characters. If you
overwrite a longer string (such as “This is a test of the OpenWrite
method”) with a shorter string (such as “Second run”), the file will
contain a mix of the strings (“Second runtest of the OpenWrite
method”).
所以这就是您得到损坏文件的原因。您正在用更新的 key/value 对覆盖前 N 个字节,文件的其余部分是原始文件中的重复数据。您可能想改用以下内容,这将完全覆盖原始文件:
using (StreamWriter sw = new StreamWriter(file, false))
我正在尝试读取 C++ header 中的每个 #define
,并为每个值显示一个编辑器,以便我可以快速重新定义常量。
我正在尝试解析文件,将左侧的每个项目作为关键。我想在左侧显示值,编辑值并保存回来。目前,当我保存我的文件时,它弄乱了格式。
这是我目前拥有的代码:
private String header;
private Dictionary<String, String> tokens;
private String file = Properties.Settings.Default.path_location;
public TextBox elf_naparm_zombie_developer;
public harrs_gsh_editor(string file)//TextBox elf_naparm_zombie_developer
{
Properties.Settings.Default.path_location = file;
Properties.Settings.Default.Save();
loadweapon();
// this.elf_naparm_zombie_developer = elf_naparm_zombie_developer;
}
public void loadweapon()
{
this.tokens = File.ReadLines(this.file)
.Select(line => Regex.Match(line, @"^\s*#define\s+(.+?)(\s+(.+?))?((\s*//)|$)"))
.Where(m => m.Success && m.Groups[3].Success)
.ToDictionary(m => m.Groups[1].Value, m => m.Groups[3].Value);
}
public String Search(String name)
{
foreach (KeyValuePair<String, String> pair in tokens)
{
if (pair.Key == name)
return pair.Value;
}
return null;
}
public void Set(String key, String val)
{
tokens[key] = val;
}
public void Save(String file)
{
using (StreamWriter sw = new StreamWriter(File.OpenWrite(file)))
{
sw.Write(header);
foreach (KeyValuePair<String, String> pair in tokens)
{
sw.Write("\r\n" + pair.Key + "#define " + pair.Value);
}
}
}
public void Save()
{
this.Save(file);
}
我遇到的问题是当我将它保存回来时,它弄乱了文件并删除了#define。我想做的只是更改值并将其用更改后的值写回程序
这是file i want to edit
所以我想做的是加载选定的键并显示值 我已经设法用我创建的这段代码来做,但我遇到的问题是把它写回去
我用来加载和填充文本框的代码
namespace Harry_s_Template_Editor
{
public partial class Form1 : Form
{
harrs_gsh_editor elfghc;
string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"/hb21_zm_ai_napalm.gsh";
public Form1()
{
InitializeComponent();
elfghc = new harrs_gsh_editor(path);//TextBox elf_naparm_zombie_developer
elf_naparm_zombie_developer = elf_naparm_zombie_developer;
// MessageBox.Show(path);
}
private void button1_Click(object sender, EventArgs e)
{
elfghc.loadweapon();
this.elf_naparm_zombie_developer.Text = elfghc.Search("NAPALM_ZOMBIE_DEVELOPER_DEBUG_PRINTS");
elfghc.Set("NAPALM_ZOMBIE_DEVELOPER_DEBUG_PRINTS", elf_naparm_zombie_developer.Text);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
elfghc.Set("NAPALM_ZOMBIE_DEVELOPER_DEBUG_PRINTS", elf_naparm_zombie_developer.Text);
elfghc.Save(path);
}
}
}
当我保存文件时,当我尝试再次加载程序时出现此错误 error
在此先感谢 Yuki
原回答:
为什么要按以下顺序将其写回文件:sw.Write("\r\n" + pair.Key + "#define " + pair.Value);
? #define
不应该排在 \r\n
之后吗?我原以为应该是这样的:sw.Write("\r\n#define " + pair.Key + " " + pair.Value);
新答案(问题已更改)
你的操作有两个问题:
您正在将 key/value 对写回文件,忽略原始文件中的 headers 和制作者名单。如果您乐于将它们剥离,那很好,但听起来这对您很重要。因此,您需要读取文件,找到文件中的键,然后更新值并将更新后的文件保存回磁盘。这比简单地将一堆 key/value 对写入文件更难。
第二个问题是您将文件写回磁盘的方式。根据 this MSDN 页面,
File.OpenWrite
具有以下行为:
The OpenWrite method opens a file if one already exists for the file path, or creates a new file if one does not exist. For an existing file, it does not append the new text to the existing text. Instead, it overwrites the existing characters with the new characters. If you overwrite a longer string (such as “This is a test of the OpenWrite method”) with a shorter string (such as “Second run”), the file will contain a mix of the strings (“Second runtest of the OpenWrite method”).
所以这就是您得到损坏文件的原因。您正在用更新的 key/value 对覆盖前 N 个字节,文件的其余部分是原始文件中的重复数据。您可能想改用以下内容,这将完全覆盖原始文件:
using (StreamWriter sw = new StreamWriter(file, false))