| | |
error with stream
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2009
Posts: 19
Reputation:
Solved Threads: 0
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;
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();
}•
•
Join Date: Oct 2009
Posts: 92
Reputation:
Solved Threads: 8
0
#2 34 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.
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; 34 Days Ago at 2:33 pm. Reason: typo
•
•
Join Date: Oct 2009
Posts: 92
Reputation:
Solved Threads: 8
0
#5 34 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:
Here's that same test without the "FileStream"
Both work.
Here's the test I did:
C# Syntax (Toggle Plain Text)
public static void doWrite() { FileStream file = new FileStream(@"c:\science\doWrite.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter(file); sw.Write("Hello file system world!"); sw.Close(); file.Close(); }
Here's that same test without the "FileStream"
C# Syntax (Toggle Plain Text)
public static void doWrite2() { StreamWriter sw = new StreamWriter(@"c:\science\doWrite.txt"); sw.Write("Hello file system world!"); sw.Close(); }
Both work.
![]() |
Similar Threads
- Problem using Windows commands in a Java Networking application (Java)
- Launching Executables within java (Java)
- question about compiling (Java)
- How to kill the running .jar file from another jar in java? (Java)
- Why would this excpetion handling is not working (C++)
- Help with Creating a Command Line Menu (C++)
- read data written to an output stream (Java)
- how to eliminate all keyboard inputs accept a certain range (C)
- Colour (Java)
- Multiple data being read in (C++)
Other Threads in the C# Forum
- Previous Thread: Mouse Events Handling
- Next Thread: Add user control in application
| Thread Tools | Search this Thread |
.net access ado.net algorithm alignment array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom customactiondata database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ hospitalmanagementsystems httpwebrequest image index input install java label list listbox listener mandelbrot math mouseclick mysql networking operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






