Question

I have a program that I need to have a config file with value to be displayed in my program. Inside my text file I have Wireless = 1 & Cradle = 2.

In my program I will have a label populate the release number only and not the other characters.

Was it helpful?

Solution

private string searchFile(String path, String searchText)
{
      string regex=@"(?i)(?<="+searchText+@"\s*=\s*)\d+";
      return Regex.Match(File.ReadAllText(path),regex).Value;//version number
}

This is what I tried and it gives the correct output

string s="Wireless = 1 Cradle = 2";
Regex.Match(s,@"(?i)(?<=Wireless\s*=\s*)\d+").Value;//1

OTHER TIPS

public static string match;

    public static string ReadAllText(string path)
    {
        using (var r = new System.IO.StreamReader(path))
        {
            return r.ReadToEnd();
        }


    }

    private string Wireless(String path, String searchText)
    {
        string regex = @"(?i)(?<=" + searchText + @"\s*=\s*)\d+";
        match = Regex.Match(ReadAllText(path), regex).Value;
        label1.Text = match;
        return match;


    }
    private string Cradle(String path, String searchText)
    {
        string regex = @"(?i)(?<=" + searchText + @"\s*=\s*)\d+";
        match = Regex.Match(ReadAllText(path), regex).Value;
        label2.Text = match;
        return match;
    }




    private void button1_Click(object sender, EventArgs e)
    {
     Wireless(@"\Storage Card\changelog.txt","Wireless");
     Cradle(@"\Storage Card\changelog.txt", "Cradle");

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top