open filestream
-
I got this piece of code from, I know not where, as a recommended way to write to a file.
// better way to write to file stream
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(textBox1.Text);
sw.Close();
}Any thoughts?
-
I got this piece of code from, I know not where, as a recommended way to write to a file.
// better way to write to file stream
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(textBox1.Text);
sw.Close();
}Any thoughts?
-
electriac wrote:
Any thoughts?
My first thought is, "what is your question"?
Veni, vidi, abiit domum
-
The term "correct" would be dependent upon the codes overall purpose. Since we know nothing of what you're doing or why, it's impossible to answer the question.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
I got this piece of code from, I know not where, as a recommended way to write to a file.
// better way to write to file stream
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(textBox1.Text);
sw.Close();
}Any thoughts?
-
I got this piece of code from, I know not where, as a recommended way to write to a file.
// better way to write to file stream
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(textBox1.Text);
sw.Close();
}Any thoughts?
Since you're just overwriting the file if it exists, try:
File.WriteAllText(fileName, textBox1.Text);
http://msdn.microsoft.com/en-us/library/system.io.file.writealltext.aspx[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer