如何解析文本文件C#,并得到正确的结果数据

问题描述 投票:1回答:2

我解析一个文本文件中获取设置数据蓝牙:

这个值可以被禁用或启用,有*标记。

我遇到的问题是,它一如既往的返回状态“已启用”,如果不管其禁用。

难道这是由于该文本阅读其从具有在被缩进该行的前一个标签。我怎样才能解决这个工作?

  private String GetBluetoothStatus(String FilePath)
{
    String[] lines = File.ReadAllLines(FilePath);
    int index = 0;
    string status = "";
    foreach (String s in lines)
    {
        if (s == "Bluetooth")
        {
            if (lines[index + 1].StartsWith("*"))
                status = "Disabled";
            else
                status = "Enabled";
            return status;
        }
        index += 1;
    }
    return status;
}

该文本文件低于:

BIOSConfig 1.0
;
;     Originally created by BIOS Configuration Utility
;     Version: 4.0.25.1
;     Date="2018/08/06" Time="15:42:35" UTC="-5"
;
;     Found 182 settings
;
Power On When AC Detected
    *Disabled
    Enabled
Power On When Lid is Opened
    *Disabled
    Enabled
Fast Boot
    *Disable
    Enable
Bluetooth
    Disabled
    *Enabled
Wireless Network Device (WLAN)
    Disabled
    *Enabled
LAN / WLAN Auto Switching
    Disabled
    *Enabled
c# parsing
2个回答
3
投票

更改以下行

if (lines[index + 1].StartsWith("*"))

  if (lines[index + 1].Trim().StartsWith("*Disabled"))

更好地利用qazxsw POI像你刚才提到qazxsw POI Contains也出现在同一行以下。

Enable

1
投票

你做Disable但如果我们看看你的配置文件将其与空间/制表开始。

尝试测试if (lines[index + 1].Contains("*Disabled")) 之前删除这个空格/制表检查值。

如果修剪不工作,尝试lines[index + 1].StartsWith("*")

© www.soinside.com 2019 - 2024. All rights reserved.