我正在开发简单的应用程序,它选择文件并读取该文本文件,我想删除所有以`#comments 行开头的行,这是代码
private void button1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = ".txt File Detector";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "txt files (*.txt)|*.txt";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
input = fdlg.FileName;
textBox1.Text = fdlg.FileName;
}
}
catch (Exception eee)
{
MessageBox.Show(eee.ToString());
}
string taxt = File.ReadAllText(input);
string[] lines = new string[100000];
//string taxt = File.ReadAllText(input);
while( taxt != null)
{
int count = 0;
if (taxt.Trim().StartsWith("#") != true)
{
lines[count] = taxt;
richTextBox1.Text += lines.ToString();
count++;
}
}
我也尝试将正则表达式用作分隔符,但这在以 # 开头的注释行的正则表达式为:
"@^#" 并在删除这些注释行后,我想存储在 arraylist 中