i am working on simple application which select the file and read that text file and i want to delete all lines which start with `#comments line and here is code
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++;
}
}
i also tried to do with Regular expression as delimeter but that doesn’t work where regex for comments line which start with # is :
“@^#”
and after removing these comments line i want to store in arraylist
Your code has a number of problems:
taxtis non-null, but never changing its valuestring[].ToStringon each iteration, for no obvious reasonYou want something like:
However, you shouldn’t be doing the file handling in the UI thread. Do you actually want to show the result in a
RichTextBox, or was that just for debugging?