我有一个程序,我需要有一个值为的配置文件 显示在我的程序中。在我的文本文件中,我有Wireless = 1 &安培;摇篮= 2.
在我的程序中,我将只有一个标签填充版本号而不是另一个 字符。
答案 0 :(得分:1)
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
答案 1 :(得分:0)
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");
}