c# - 如何解析文本文件c#并获得正确的结果数据
问题描述
我正在解析一个文本文件以获取蓝牙的设置数据:
该值可以被禁用或启用并用 * 标记。
我遇到的问题是它总是返回状态为“启用”,不管它是否被禁用。
这可能是因为它读取的文本在缩进的行前面有一个制表符。我怎样才能纠正这个工作?
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
解决方案
更改以下行
if (lines[index + 1].StartsWith("*"))
至
if (lines[index + 1].Trim().StartsWith("*Disabled"))
Contains
像你提到的那样更好地使用也Enable
Disable
可以出现在同一行中。
if (lines[index + 1].Contains("*Disabled"))
推荐阅读
- sql - 如何制作一个从 SQL 表中读取并修改它们的脚本?
- spring - 未触发特定异常的 RetryTemplate
- json - 如何查询sqlite json1
- python - 使用 python、regex 和 join() 以奇数间隔连接数组中的不同项目
- linux - 如何在 Rundeck 和 Windows Server 之间建立连接?
- python - '>' 在 'str' 和 'int' 的实例之间不支持我在 excel 中写入时出错
- java - Sling Rewriter 更改静态资源 URL
- python - 图像“pyimage4”不存在
- node.js - Sqlite 返回值与数据库不同
- react-native - 使用导航和处理管理屏幕来响应本机提交表单数据