2016-01-08 54 views
0

我有一个.resx文件包含字符串名称值对。现在我想使用C#(Windows窗体)实用地将名称和值对变为List。我怎样才能做到这一点。但是这里列出了价值成就的一个转折点,我有一个“组合框”和两个文本框。在运行时,应将所有密钥添加到组合框中,并自动将其他两个测试框填入值和注释。请帮助我完成这项任务。 在此先感谢...从.resx文件中的资源读取键值,值和注释

+0

你是什么意思与“列表中的我有一个‘组合框’和两个文本框”?请澄清你的问题,如果可能的话发布一些代码。 :) – LucaMus

+0

嗨@LucaMus我想追加所有的键到组合框中,并对应每个键值和注释出现在resx文件中。我希望当我通过comboBox选择一个键时,该键的值和注释会自动出现在带有值的textBox1和带有通信的textbox2中 – VIVEK

回答

1

看看ResXResourceReader,这可以很容易地做你想做的事情。

例如,你可以这样做:

private void Form1_Load(object sender, EventArgs e) 
    { 
     //ComboBox will use "Name" property of the items you add 
     comboBox1.DisplayMember = "Name"; 
     //Create the reader for your resx file 
     ResXResourceReader reader = new ResXResourceReader("C:\\your\\file.resx"); 
     //Set property to use ResXDataNodes in object ([see MSDN][2]) 
     reader.UseResXDataNodes = true; 
     IDictionaryEnumerator enumerator = reader.GetEnumerator(); 

     while (enumerator.MoveNext()) 
     { //Fill the combobox with all key/value pairs 
      comboBox1.Items.Add(enumerator.Value); 
     } 
    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboBox1.SelectedIndex == -1) 
      return; 

     //Assembly is used to read resource value 
     Assembly currentAssembly = Assembly.GetExecutingAssembly(); 
     //Current resource selected in ComboBox 
     ResXDataNode node = (ResXDataNode)comboBox1.SelectedItem; 

     //textBox2 contains the resource comment 
     textBox2.Text = node.Comment; 
     //Reading resource value, you can probably find a smarter way to achieve this, but I don't know it 
     object value = node.GetValue(new AssemblyName[] { currentAssembly.GetName() }); 
     if (value.GetType() != typeof(String)) 
     { //Resource isn't of string type 
      textBox1.Text = ""; 
      return; 
     } 

     //Writing string value in textBox1 
     textBox1.Text = (String)value; 
    } 
相关问题