我使用了一个代码但得到一个错误,即
扩展方法必须在顶级静态类中定义;StringHelpers
是一个嵌套类
namespace Figreplace2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FileInfo n = new FileInfo(textBox1.Text);
StringBuilder newFile = new StringBuilder();
string temp = " ";
string[] file = File.ReadAllLines(textBox1.Text);
}
public static class StringHelpers
{
public static string Replace(this string s)
{
Dictionary<string, string> replacements = new Dictionary<string, string>();
//replacements.Add("ID1", "NewValue");
replacements.Add("ID2", "NewValue2");
// ... further replacement entries ...
foreach (string line in file)
{
bool replacementMade = false;
foreach (var replacement in replacements)
{
if (line.StartsWith(replacement.Key))
{
string newString = s;
temp = line.Replace(string.Format("{0} :{1}", replacement.Key, replacement.Value));
newFile.Append(temp + "\r\n");
continue;
return newString;
}
newFile.Append(line + "\r\n");
replacementMade = true;
// break;
}
if (!replacementMade)
{
File.WriteAllText(@"D:\madhu\test2\23.txt", newFile.ToString());
}
}
}
}
}