我有一个程序,我需要一个配置文件,其值要显示在我的程序中。在我的文本文件中,我有 Wireless = 1 & Cradle = 2。
在我的程序中,我将有一个标签仅填充版本号,而不是其他字符。
private string searchFile(String path, String searchText)
{
string regex=@"(?i)(?<="+searchText+@"\s*=\s*)\d+";
return Regex.Match(File.ReadAllText(path),regex).Value;//version number
}
这是我尝试过的,它给出了正确的输出
string s="Wireless = 1 Cradle = 2";
Regex.Match(s,@"(?i)(?<=Wireless\s*=\s*)\d+").Value;//1
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");
}