I have a project started where a Windows form application searches for a specified directory (and any changes there of using a filemonitor) and lists the files in a datagridview. Each file found is linked to the file location and the resulting file (in this case text files) is rendered to a textbox. Currently the cellclickevent displays the file name of the file. What I would like to do is search the contents of the file for a specific line and character location. In my example, I need to display the text displayed on line 3 of the file starting at character 8 (Line3, Char 8), and only look for a maximum of say 40 characters beyond the 8th character position. This character string will represent a name of a person, usually in this format "Lastname, Firstname".
So as opposed to displaying the file name in the gridview (fi.Name), I want to swap the characters for the persons name as it appears on line 3, char 8 through 48. Is anyone familiar with how to do this? I still need to maintain the file link in the cellclick event. Any help here is greatly appreciated. Here is the code I'm using for these gridview which is bound to the filemonitor, and the cellclick event:
public void DirMon_Load(object sender, System.EventArgs e)
{
FileMonitor.Path = Path.ToString();
FileMonitor.Filter = Filter.ToString();
FileMonitor.IncludeSubdirectories = IncludeSubs;
FileMonitor.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
//Begin monitoring.
FileMonitor.EnableRaisingEvents = true;
//Get the list of existing files from the folder and list in the textbox
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\test");
//to retrieve *.TXT files alone
System.IO.FileInfo[] rgFiles = di.GetFiles("*.txt");
foreach (System.IO.FileInfo fi in rgFiles)
{
int index = dataGridView1.NewRowIndex;
dataGridView1.Rows.Add(fi.Name);
}
dataGridView1_CellContentClick(null, null);
}
public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
richTextBox1.Text = "";
if (dataGridView1.CurrentCell.OwningColumn.Name.Equals("File"))
{
if (dataGridView1.CurrentCell != null)
{
string filename = dataGridView1.CurrentCell.Value.ToString();
string fullpath = string.Format("{0}\\{1}", Path, filename);
StreamReader file = null;
try
{
file = new StreamReader(fullpath);
richTextBox1.LoadFile(fullpath,
RichTextBoxStreamType.PlainText);
// this is a hidden text box that the user does not see, used for printing
HiddentextBox1.Text = fullpath;
}
finally
{
if (file != null)
file.Close();
}
}
}
}
Loading
JosePosted Dec 30, 2009, 10:41 AM
public void DirMon_Load(object sender, System.EventArgs e)
{
FileMonitor.Path = Path.ToString();
FileMonitor.Filter = Filter.ToString();
FileMonitor.IncludeSubdirectories = IncludeSubs;
FileMonitor.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
//Begin monitoring.
FileMonitor.EnableRaisingEvents = true;
//Get the list of existing files from the folder and list in the textbox
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Path);
//to retrieve *.TXT files alone
System.IO.FileInfo[] rgFiles = di.GetFiles("*.txt");
foreach (System.IO.FileInfo fi in rgFiles)
{
string files = (Path + "\\" + fi.Name);
int index = dataGridView1.NewRowIndex;
MessageBox.Show(Path + "\\" + fi.Name);
using (StreamReader sr = new StreamReader(files))
{
sr.ReadLine();
sr.ReadLine();
String Record = sr.ReadLine();
dataGridView1.Rows.Add(Record.Substring(7, 35));
}
}
dataGridView1_CellContentClick(null, null);
}
public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
richTextBox1.Text = "";
if (dataGridView1.CurrentCell.OwningColumn.Name.Equals("File"))
{
if (dataGridView1.CurrentCell != null)
{
string filename = dataGridView1.CurrentCell.Value.ToString();
string fullpath = string.Format("{0}\\{1}", Path, filename);
StreamReader file = null;
//string line;
try
{
//Here is where the debugger tells me the file is not found
file = new StreamReader(fullpath);
richTextBox1.LoadFile(fullpath,
RichTextBoxStreamType.PlainText);
// Hidden text box needed here for printing routine
HiddentextBox1.Text = fullpath;
}
finally
{
if (file != null)
file.Close();
}
}
}
}
I tried adding a new column to the gridview and then changed the line of code to dataGridView1.Rows.Add(Record.Substring(7, 35), fi.Name);, which at least lets the program run. I can only load the file when I click on the file name. I want to have a single column represented by the user name and click-able so that it links to the actual file and loads into the richtextbox (maybe some masking?). Any ideas on how I can accomplish this? Thanks
JosePosted Dec 30, 2009, 8:54 AM
Sam HobbsPosted Dec 29, 2009, 11:41 PM
JosePosted Dec 28, 2009, 3:46 PM
dataGridView1.Rows.Add(fi.Name);
Thanks.
Sam HobbsPosted Dec 28, 2009, 3:28 PM