| | |
error with stream
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2009
Posts: 16
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: 91
Reputation:
Solved Threads: 8
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.
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
•
•
Join Date: Oct 2009
Posts: 91
Reputation:
Solved Threads: 8
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:
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 algorithm angle array barchart bitmap box broadcast c# capturing check checkbox client combobox control conversion convert csharp custom database datagrid datagridview dataset datetime dbconnection degrees delegate design development disappear draganddrop drawing encryption enum event excel file firefox form format forms function gdi+ httpwebrequest image index input install java label leak libraries list listbox mandelbrot math monodevelop mouseclick msword mysql operator path pause photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox round server sleep socket sql statistics stream string table tcpclientchannel text textbox thread time timer update usercontrol validation virtualization visualbasic visualstudio webbrowser windows winforms wpf xml






