944,014 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 479
  • C# RSS
Nov 4th, 2009
0

error with stream

Expand Post »
Hi, why i can read and write to file?
Show error only on the "file". Any other well, but I can not read or write.

I use: using System.IO;
private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.ShowDialog();
            textBox1.Text = of.FileName;

            file = new FileStream(textBox1.Text, FileMode.OpenOrCreate, FileAccess.Read);

            StreamReader sr = new StreamReader(file);

            string s = sr.ReadToEnd();

            sr.Close();

            file.Close();
                       
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog of = new SaveFileDialog();
            of.ShowDialog();
            textBox2.Text = of.FileName;

            MessageBox.Show(textBox2.Text);

            FileStream file = new FileStream(textBox2.Text, FileMode.OpenOrCreate, FileAccess.Write);

            StreamWriter sw = new StreamWriter(file);

            sw.Write("Hello file system world!");

            sw.Close();

            file.Close();
      
        }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Lolalola is offline Offline
74 posts
since Jul 2009
Nov 4th, 2009
0
Re: error with stream
So, on your READ, you want to create an empty file if one does not exist?

Also, if you put a try/catch block around it, the problem could be revealed.

Also, StreamReader and StreamWriter will take a filename as a parameter, so you can avoid the extra step of the "File" creation.
Last edited by thines01; Nov 4th, 2009 at 2:33 pm. Reason: typo
Featured Poster
Reputation Points: 290
Solved Threads: 277
Posting Virtuoso
thines01 is online now Online
1,689 posts
since Oct 2009
Nov 4th, 2009
0
Re: error with stream
when I write want to create a new file.
But why not work now writing the file did not understand
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Lolalola is offline Offline
74 posts
since Jul 2009
Nov 4th, 2009
0
Re: error with stream
>>StreamWriter sw = new StreamWriter(file);
IMO a StreamWriter expects a filename as of.FileName, not a FileStream like file.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Nov 4th, 2009
0
Re: error with stream
The technique you use for the write WILL work. Maybe it was the way the input string was formatted from the Dialog Box (?)

Here's the test I did:

C# Syntax (Toggle Plain Text)
  1. public static void doWrite()
  2. {
  3. FileStream file = new FileStream(@"c:\science\doWrite.txt", FileMode.OpenOrCreate, FileAccess.Write);
  4. StreamWriter sw = new StreamWriter(file);
  5. sw.Write("Hello file system world!");
  6. sw.Close();
  7. file.Close();
  8. }

Here's that same test without the "FileStream"
C# Syntax (Toggle Plain Text)
  1. public static void doWrite2()
  2. {
  3. StreamWriter sw = new StreamWriter(@"c:\science\doWrite.txt");
  4. sw.Write("Hello file system world!");
  5. sw.Close();
  6. }

Both work.
Featured Poster
Reputation Points: 290
Solved Threads: 277
Posting Virtuoso
thines01 is online now Online
1,689 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Mouse Events Handling
Next Thread in C# Forum Timeline: Add user control in application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC