By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,244 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,244 IT Pros & Developers. It's quick & easy.

C#-> Open a File... Problem

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

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
On Fri, 8 Oct 2004 00:32:39 -0400, "Hareth" <ab******@hotmail.com>
wrote:
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


Nov 16 '05 #2

P: n/a
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
"JuLiE Dxer" <Ms*****@verizon.net> wrote in message
news:ra********************************@4ax.com...

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
On Fri, 8 Oct 2004 00:32:39 -0400, "Hareth" <ab******@hotmail.com>
wrote:
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

Nov 16 '05 #3

P: n/a
Hareth <ab******@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a

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

On Fri, 8 Oct 2004 01:50:25 -0400, "Hareth" <ab******@hotmail.com>
wrote:
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
"JuLiE Dxer" <Ms*****@verizon.net> wrote in message
news:ra********************************@4ax.com.. .

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
On Fri, 8 Oct 2004 00:32:39 -0400, "Hareth" <ab******@hotmail.com>
wrote:
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


Nov 16 '05 #5

P: n/a
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;

Nov 16 '05 #6

P: n/a
Thanx Julie,
thats exactly what I needed

"JuLiE Dxer" <Ms*****@verizon.net> wrote in message
news:kl********************************@4ax.com...

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

On Fri, 8 Oct 2004 01:50:25 -0400, "Hareth" <ab******@hotmail.com>
wrote:
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
"JuLiE Dxer" <Ms*****@verizon.net> wrote in message
news:ra********************************@4ax.com. ..

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
On Fri, 8 Oct 2004 00:32:39 -0400, "Hareth" <ab******@hotmail.com>
wrote:

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

Nov 16 '05 #7

P: n/a

welcome :)

On Fri, 8 Oct 2004 12:24:32 -0400, "Hareth" <ab******@hotmail.com>
wrote:
Thanx Julie,
thats exactly what I needed

"JuLiE Dxer" <Ms*****@verizon.net> wrote in message
news:kl********************************@4ax.com.. .

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

On Fri, 8 Oct 2004 01:50:25 -0400, "Hareth" <ab******@hotmail.com>
wrote:
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
"JuLiE Dxer" <Ms*****@verizon.net> wrote in message
news:ra********************************@4ax.com ...

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
On Fri, 8 Oct 2004 00:32:39 -0400, "Hareth" <ab******@hotmail.com>
wrote:

>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
>


Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.