1

I am parsing a text file to get settings data for Bluetooth:

This value could be disabled or enabled and marked with an *.

The problem I am having is that its always returning status as "Enabled" regardless if its disabled or not.

Could this be due to that text its reading from has a tab in front of the lines that are indented. How can I correct this to work?

  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;
}

The text file is below:

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
1
  • Suggestion. If you want to track of index, then use for not foreach loop Commented Feb 4, 2019 at 13:33

2 Answers 2

3

Change following line

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

to

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

Better use Contains like follwoing as you have mentioned that Enable Disable can appear in same line also.

if (lines[index + 1].Contains("*Disabled"))
Sign up to request clarification or add additional context in comments.

1 Comment

if the text reads : Bluetooth *Disabled Enabled will the same line work? the setting could be either way and I need it to read the one marked currently with * when the text file is generated?
1

You do lines[index + 1].StartsWith("*") but if we look your config file it begin with space/tabulation.

Try to test lines[index + 1].Trim().StartsWith("*") to remove this blank space/tabulation before check the value.

If trim don't work, try lines[index + 1].Replace("\t", " ").Replace(" ","").StartsWith("*");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.