error with stream

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 16
Reputation: Lolalola is an unknown quantity at this point 
Solved Threads: 0
Lolalola Lolalola is offline Offline
Newbie Poster

error with stream

 
0
  #1
24 Days Ago
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();
      
        }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 91
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #2
24 Days Ago
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; 24 Days Ago at 2:33 pm. Reason: typo
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 16
Reputation: Lolalola is an unknown quantity at this point 
Solved Threads: 0
Lolalola Lolalola is offline Offline
Newbie Poster
 
0
  #3
24 Days Ago
when I write want to create a new file.
But why not work now writing the file did not understand
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,927
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
0
  #4
24 Days Ago
>>StreamWriter sw = new StreamWriter(file);
IMO a StreamWriter expects a filename as of.FileName, not a FileStream like file.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 91
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #5
24 Days Ago
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:

  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"
  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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC