473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C#-> Open a File... Problem

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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephen | last post by:
could someone give me some advise on a problem I have. At present i have a web application which displays pdf files in a web browser using javascript and the window.open method and pointing to a...
1
by: Bernavich2004 | last post by:
Ok I am going nuts. This problem ONLY happens when I use a Microsoft Office 2003 product like word, or .NET development IDE. WHen I use the Open file dialog and click on a folder to drill down,...
1
by: William Starr Moake | last post by:
Another problem with the browser-based WYSIWYG editor I'm trying to assemble using IE's design mode. This opens the selected page in the editor iframe named iView: <select name="template"...
1
by: Jonatan | last post by:
Hi all, I'm trying to upload files doing something like gmail. I almost got it but I have a problem when I post the form. Basicaly, I am doing the following. When the user presses the link...
5
by: Mr Gordonz | last post by:
Hi all, I want to put a button on a page, and when the user clicks it, the standard Windows "Open File" dialogue box opens, and the user can browse/select a file on their PC. Having selected a...
18
by: mollyf | last post by:
I just installed SQL Server 2005 on my PC (the developer's edition) yesterday. I have some scripts written by one of my coworkers to create some tables and stored procedures in a database that...
2
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes echo...
5
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a log, I will open the file and append to the end....
4
by: Wayne | last post by:
How do I get rid of the generic Windows "Open File - Security Warning" that appears when I try to open a database that resides on another PC on my home network? This is not the annoying macro...
4
by: pablocruise77 | last post by:
We have two Perl script that use the open command to read an encrypted file. We just changed from gzip to gpg. One script works fine. The other is called from the web and fails on the open. ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.