【问题标题】:How can i read all the lines from a text file in specific places and use the data?如何从特定位置的文本文件中读取所有行并使用数据?
【发布时间】:2015-04-21 05:36:03
【问题描述】:
string[] lines = File.ReadAllLines(@"c:\wmiclasses\wmiclasses1.txt");
   for (int i = 0; i < lines.Length; i++)
     {
        if (lines[i].StartsWith("ComboBox"))
           {

           }
      }

这就是文本文件内容的样子:

ComboBox Name cmbxOption

Classes Win32_1394Controller
Classes Win32_1394ControllerDevice

ComboBox Name cmbxStorage

Classes Win32_LogicalFileSecuritySetting
Classes Win32_TapeDrive

我需要做的是一些事情:

  1. 每次该行以ComboBox 开头,然后仅从该行获取ComboBox 名称,例如cmbxOption

由于我的form1 设计器中已经有这个ComboBoxes,我需要确定cmbxOption 的开始和结束位置以及下一个ComboBox 的开始时间cmbxStorage

  1. 获取当前ComboBox的所有行,例如这些行属于cmbxOption

    类 Win32_1394Controller Win32_1394ControllerDevice 类

  2. 例如从每一行创建一个键和值:

    类 Win32_1394Controller

那么key就是Win32_1394Controller,value就是1394Controller

然后是第二行键Win32_1394ControllerDevice 和值只有1394ControllerDevice

  1. 要将值1394Controller 添加到正确的所属组合框中。

  2. 要做到这一点,当我在 ComboBox 中选择时,例如在 cmbxOption 中选择 1394Controller 项目时,它的行为就像我选择了 Win32_1394Controller

以本次活动为例:

 private void cmbxOption_SelectedIndexChanged(object sender, EventArgs e)
    {
    InsertInfo(cmbxOption.SelectedItem.ToString(), reflstDisplayHardware, chkHardware.Checked);

    }

需要SelectedItem 将是Win32_1394Controller,但用户只能在cmbxOption 中看到1394Controller 而没有Win32_

这是InsertInfo方法的开始

  private void InsertInfo(string Key, ref ListView lst, bool DontInsertNull)
            {

这就是为什么我需要密钥是 Win32_1394Controller 但我希望用户将在 ComboBox 中看到只有 1394Controller 而没有 Win32_

【问题讨论】:

  • 太棒了。到目前为止,您尝试了什么,您的具体问题是什么? stackoverflow 不是免费为您完成工作,而是帮助程序员解决特定问题。
  • 在此处添加您的努力以表明您已经尝试过(至少)。这个地方不是为了雇人来完成你的工作。

标签: c# .net winforms


【解决方案1】:

这里是一些快速丑陋的代码:

class Group<T> : List<T>, IGrouping<T, T>
{
    public T Key { get; private set; }

    public Group(T key)
    {
        Key = key;
    }
}

static class Extensions
{
    public static IEnumerable<IGrouping<string, string>> GroupByComboboxName(this IEnumerable<string> lines)
    {
        Group<string> group = null;

        foreach (var line in lines)
        {
            if (string.IsNullOrWhiteSpace(line)) continue;

            var parts = line.Split(' ');
            if (parts.Length == 3 && parts[0] == "ComboBox" && parts[1] == "Name")
            {
                if (group != null) yield return group;
                group = new Group<string>(parts[2]);
            }
            else if (group != null)
            {
                group.Add(line);
            }
        }

        if (group != null) yield return group;
    } 
}

使用方法:

var lines = @"ComboBox Name cmbxOption

Classes Win32_1394Controller
Classes Win32_1394ControllerDevice

ComboBox Name cmbxStorage

Classes Win32_LogicalFileSecuritySetting
Classes Win32_TapeDrive".Split('\n').Select(l => l.TrimEnd('\r')).ToArray();

lines.GroupByComboboxName().ForEach(g =>
{
    Console.WriteLine(g.Key + ":");
    g.ForEach(l => Console.WriteLine("    " + l));
});

以及它生成的输出:

cmbxOption:
    Classes Win32_1394Controller
    Classes Win32_1394ControllerDevice
cmbxStorage:
    Classes Win32_LogicalFileSecuritySetting
    Classes Win32_TapeDrive

这应该能让你继续前进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多