new to streamreader

  • Thread starter Thread starter blah
  • Start date Start date
B

blah

what i want to do is read in a .ged file and find all the names in the file
and then add them to a listbox. this is what i have and it works but what
can i do better?

thanks,
Rob


public class Gedcom
{
private string gedFile;
private StreamReader fstream;
public Gedcom()
{
//
// TODO: Add constructor logic here
//
}
public string[] GetIndex()
{
//
if (fstream == null)
{
MessageBox.Show("Error opening file stream.", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
return null;
}
StreamReader fstreamCopy = fstream;
string textLine;
int index;
bool foundName = false;
ArrayList tmpIndex = new ArrayList();
while (fstreamCopy.BaseStream != null)
{
foundName = false;
try
{
textLine = fstreamCopy.ReadLine().Trim();
index = textLine.IndexOf("INDI");
if (index != -1)
{
while (!foundName)
{
textLine = fstreamCopy.ReadLine().Trim();
index = textLine.IndexOf("NAME");
if (index != -1)
{
tmpIndex.Add(textLine.Substring(index + 4).Trim());
foundName = true;
}
}
}
}
catch
{
fstreamCopy.Close();
}
}
string[] indIndex = new String[tmpIndex.Count];
for (int i = 0; i < tmpIndex.Count; i++)
{
indIndex = tmpIndex.ToString();
}
return (indIndex);
//
}
public string OpenGed( string FileName )
{
//
gedFile = FileName;
fstream = File.OpenText(gedFile);
return gedFile;
//
}
}


.....


private void mnuIndex_Click(object sender, System.EventArgs e)
{
frmIndex frmIndex = new frmIndex();
string[] index = ged.GetIndex();
frmIndex.Names = new string[index.Length];
index.CopyTo(frmIndex.Names, 0);
if (frmIndex.ShowDialog(this) == DialogResult.OK)
{
}
}
 
Hi blah,

You want to do better?
Do you refer to improve performance?
I think you can visit the link below to get some information:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechar
t/html/vbtchperfopt.asp

Hope this helps.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "blah" <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: new to streamreader
| Lines: 92
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <890_a.41474$Bp2.39790@fed1read07>
| Date: Mon, 11 Aug 2003 23:52:42 -0700
| NNTP-Posting-Host: 68.98.17.94
| X-Complaints-To: (e-mail address removed)
| X-Trace: fed1read07 1060671172 68.98.17.94 (Tue, 12 Aug 2003 02:52:52 EDT)
| NNTP-Posting-Date: Tue, 12 Aug 2003 02:52:52 EDT
| Organization: Cox Communications
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
phx.gbl!newsfeed00.sul.t-online.de!t-online.de!diablo.theplanet.net!newspee
r1-gui.server.ntli.net!ntli.net!peer01.cox.net!cox.net!p01!fed1read07.POSTED
!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:175786
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| what i want to do is read in a .ged file and find all the names in the
file
| and then add them to a listbox. this is what i have and it works but what
| can i do better?
|
| thanks,
| Rob
|
|
| public class Gedcom
| {
| private string gedFile;
| private StreamReader fstream;
| public Gedcom()
| {
| //
| // TODO: Add constructor logic here
| //
| }
| public string[] GetIndex()
| {
| //
| if (fstream == null)
| {
| MessageBox.Show("Error opening file stream.", "Error",
MessageBoxButtons.OK,
| MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
| return null;
| }
| StreamReader fstreamCopy = fstream;
| string textLine;
| int index;
| bool foundName = false;
| ArrayList tmpIndex = new ArrayList();
| while (fstreamCopy.BaseStream != null)
| {
| foundName = false;
| try
| {
| textLine = fstreamCopy.ReadLine().Trim();
| index = textLine.IndexOf("INDI");
| if (index != -1)
| {
| while (!foundName)
| {
| textLine = fstreamCopy.ReadLine().Trim();
| index = textLine.IndexOf("NAME");
| if (index != -1)
| {
| tmpIndex.Add(textLine.Substring(index + 4).Trim());
| foundName = true;
| }
| }
| }
| }
| catch
| {
| fstreamCopy.Close();
| }
| }
| string[] indIndex = new String[tmpIndex.Count];
| for (int i = 0; i < tmpIndex.Count; i++)
| {
| indIndex = tmpIndex.ToString();
| }
| return (indIndex);
| //
| }
| public string OpenGed( string FileName )
| {
| //
| gedFile = FileName;
| fstream = File.OpenText(gedFile);
| return gedFile;
| //
| }
| }
|
|
| ....
|
|
| private void mnuIndex_Click(object sender, System.EventArgs e)
| {
| frmIndex frmIndex = new frmIndex();
| string[] index = ged.GetIndex();
| frmIndex.Names = new string[index.Length];
| index.CopyTo(frmIndex.Names, 0);
| if (frmIndex.ShowDialog(this) == DialogResult.OK)
| {
| }
| }
|
|
|
 
Back
Top