C#-> Open a File... Problem

H

Hareth

How do I open a file (such as .txt) in C#.....

I tried:
String File;

File = txtMain.Text;

dlgOpen.ShowDialog();

File = dlgOpen.FileName;

using (StreamReader sr = new StreamReader(File))

sr.Read(dlgOpen.FileName); <- This line generated errors

\\\\\\\\\\\\\\\\\\

For saving a file I did...

string File;

File = txtMain.Text;

dlgSave.ShowDialog();

File = dlgSave.FileName;

using (StreamWriter sw = new StreamWriter(File))

sw.Write(txtMain.Text);

It worked, so I thought I could do the same for opening a file....Anyway,
anyone has any ideas?? thanks
 
J

JuLiE Dxer

Perhaps, something more like this?

// Declare your file path

strFileName = @"C:\\txtMain.txt";

// Then on to the bigger things

FileStream objFileStream =
new FileStream( strFileName, FileMode.Open, FileAccess.Read );

BufferedStream objBufferedStream =
new BufferedStream( objFileStream );

StreamReader objStreamReader =
new StreamReader( objBufferedReader );

// Read in a line

string strLine;

strLine = objStreamReader.ReadLine();

// whatever code processing code here (if you like)

while ( strLine != null )
{
// continue to read in lines

strLine = objStreamReader.ReadLine();

// your extra code

)


Just an Example of one easy way to get it going for you to get your
feet moving.

I hope it helps at least a bit.

MsJuLiE
 
H

Hareth

can you give me another way, with an openfiledialog....
thanx
Also, do i have to declare anything? or refrence anything...b/c your code
generated errors too
 
J

Jon Skeet [C# MVP]

Hareth said:
How do I open a file (such as .txt) in C#.....

I tried:
String File;

File = txtMain.Text;

dlgOpen.ShowDialog();

File = dlgOpen.FileName;

using (StreamReader sr = new StreamReader(File))

sr.Read(dlgOpen.FileName); <- This line generated errors

Yes, it would - there's no method StreamReader.Read(string). What are
you trying to do at that point? You've already opened the right file
with the previous line.
 
J

JuLiE Dxer

You wish to have a dialog box appear from where you can choose the
file to read from ?

perhaps something similar to



private void openSource(object sender, System.EventArgs e)
{
string str_FileName; // your file name is stored in this string

OpenFileDialog fdlg = new OpenFileDialog();

fdlg.Title = "Please choose your text file." ;

fdlg.Filter = "Text Files (*.TXT)|*.TXT";

fdlg.RestoreDirectory = true ;

if(fdlg.ShowDialog() == DialogResult.OK)
{
str_FileName = fdlg.FileName ;
}

}



MsJ
 
P

Pradeep

Check whether file really exists before you use the Dialog.FileName
you can do the same by following lines

openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
 
H

Hareth

Thanx Julie,


thats exactly what I needed



JuLiE Dxer said:
You wish to have a dialog box appear from where you can choose the
file to read from ?

perhaps something similar to



private void openSource(object sender, System.EventArgs e)
{
string str_FileName; // your file name is stored in this string

OpenFileDialog fdlg = new OpenFileDialog();

fdlg.Title = "Please choose your text file." ;

fdlg.Filter = "Text Files (*.TXT)|*.TXT";

fdlg.RestoreDirectory = true ;

if(fdlg.ShowDialog() == DialogResult.OK)
{
str_FileName = fdlg.FileName ;
}

}



MsJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top