System.IO.StreamWriter : Why do I have to use keyword "using" - c#

//C#
using (System.IO.StreamWriter writer =
new System.IO.StreamWriter("me00.txt", true))
{
writer.WriteLine("Hey"); //saved
}
System.IO.StreamWriter writer02 = new System.IO.StreamWriter("me01.txt", true);
writer02.WriteLine("Now hey x2"); //not saved
File me00.txt and me01.txt are created, however only the first file's content gets saved.
me00.txt will have line hey. me01.txt will be an empty txt file; "Now hey x2" is not saved.
What does the keyword "using" do to cause this observation?

You don't have to use "using". It's just a shortcut to keep you from doing even more typing...
The alternative is to nest the whole thing in a try-finally construct like the following:
System.IO.StreamWriter writer = null;
try
{
writer = new System.IO.StreamWriter("me00.txt", true);
writer.WriteLine("Hey");
}
finally
{
if (writer != null)
writer.Dispose();
)
When the writer is disposed, it is also closed, which is the step you're missing. The using presents a tidy way to do all of this compactly.

Flush the Buffer. It works with the using statement, because the buffer is flushed when the stream being disposed/closed.
Just call writer02.Flush();
From the MSDN on StreamWriter.Flush :
Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close
Also, you need to dispose the Stream. Don't forget to call the Dispose method when you are done with the stream.
The using statement will do it for you. If you call Dispose, you might not need to call Flush.

Add:
writer02.Close();
--------------:
public void Test()
{
//C#
using (System.IO.StreamWriter writer = new System.IO.StreamWriter("me00.txt", true))
{
writer.WriteLine("Hey");
}
}
Because this code is identical to the code
public void Test()
{
//C#
System.IO.StreamWriter writer = null;
try
{
writer = new System.IO.StreamWriter("me01111.txt", true);
writer.WriteLine("Hey"); //saved
}
finally
{
if (writer != null) //Flush data
{
((IDisposable)writer).Dispose();
}
}
}

Related

How to prevent Newtonsoft JsonSerializer to close the stream?

So I haven't written the code I'm dealing right now and I'm looking how to best handle this.
Right now I have this.
public static void WriteSymbol(Stream stream, Symbol symbol)
{
using (var streamWriter = new StreamWriter(stream))
{
JsonSerializer.Create(SerializerSettings).Serialize(streamWriter, symbol);
}
}
I'd like to be able to read the content of the stream after this is done in my test so that I can check the integration. The problem is that right now after Serialize the stream is closed and I can't read anymore from it.
I see that JsonWriter public bool CloseOutput { get; set; } but I don't see something for this static JsonSerializer.
What would be the best way to go about this?
How do I prevent the Serializer from closing the stream?
Is there some way I should check the content of the Stream?
From .net 4.5 upwards you can use the LeaveOpen constructor argument of StreamWriter.
The default buffer size used by the StreamWriter is 1024 as visible when decompiling the type.
So you can do the following.
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8, 1024, true))
{
// TODO: do something
}
Try something like this:
public static void WriteSymbol(Stream stream, Symbol symbol)
{
using (var streamWriter = new StreamWriter(stream))
{
JsonSerializer.Create(SerializerSettings).Serialize(streamWriter, symbol);
// test stream here
}
}
Or don't surround this call with a using statement and then close the stream outside of this method.

Writing to a text file in AppData doesn't work - C#

I'm using the following lines of code in order to write credentials of users to a text file. It's supposed to create the directory inside AppData (which it does) but it doesn't write the credentials to the text file, it leaves it blank!
public void RegisterUserCreds()
{
string[] creds = { Username.Text, Password.Text };
string roaming = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (!Directory.Exists(roaming + "/Launcher"))
Directory.CreateDirectory(roaming + "/Launcher");
string specificFolder = roaming + "/Launcher/user_info.txt";
var fs = File.Open(specificFolder, FileMode.OpenOrCreate, FileAccess.ReadWrite);
var sw = new StreamWriter(fs);
sw.WriteLine(Username.Text);
fs.Close();
}
What's the problem? Thanks!
Just use the using statement when operating on streams:
public static void RegisterUserCreds()
{
string[] creds = { Username.Text, Password.Text };
string roaming = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (!Directory.Exists(roaming + "/Launcher")) Directory.CreateDirectory(roaming + "/Launcher");
string specificFolder = roaming + "/Launcher/user_info.txt";
using (var fs = File.Open(specificFolder, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (var sw = new StreamWriter(fs))
{
sw.WriteLine(Username.Text);
}
}
}
In your code you were closing the file stream before the stream writer was able to flush the changes you want to write so the file was created empty.
You're closing the wrong stream. When you create new stream objects and pass an existing stream to the constructor, that new stream now "owns" the old stream. When you dispose of the newer stream it will automatically dispose of the older one.
In your situation, you're closing the "fs" stream, but the "sw" stream might not have actually written to it yet (it has it's own internal buffer). If you were to close the "sw" stream instead, it would flush it's buffer (into the "fs" stream), and then it would call fs.Dispose() for you to make sure it did the same thing.
There's a much better way, that would help you avoid doing things out-of-order like this, as well as make sure you're calling Dispose() even if exceptions get thrown (streams implement IDisposable, so you're supposed to always call their Dispose() method when you're done with them so they can internally "clean up"). The using statement is perfect for this, since it will call Dispose() even if an exception is thrown (it's a shortcut for wrapping the code with try/finally blocks):
using (var fs = File.Open(specificFolder, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (var sw = new StreamWriter(fs))
{
sw.WriteLine(Username.Text);
}
}
This is the same as this:
try
{
var fs = File.Open(specificFolder, FileMode.OpenOrCreate, FileAccess.ReadWrite);
try
{
var sw = new StreamWriter(fs);
sw.WriteLine(Username.Text);
}
finally
{
sw.Dispose();
}
}
finally
{
fs.Dispose();
}
Even though sw.Dispose() will call fs.Dispose() for you, there is no harm in calling fs.Dispose() again. Why is it important to call Dispose()? Let's say an exception was thrown during sw.WriteLine() (e.g. out of disk space, I/O error, etc.)... the file would stay open until your app terminated. The using (or the try/catch version) would make sure the file was closed no matter what.
(side note: with streams, Dispose() and Close() do the same thing, you don't need to call both. Close() just calls Dispose() -- MS included a method called Close() because that was what people were used to with a file API, but the .NET IDisposable interface uses a method called Dispose())
(another side note: starting with .NET 4.5, many of the stream classes have an additional constructor that has a new "leaveOpen" parameter... passing true would tell that stream to NOT dispose of the original stream automatically)

System IO Exception : the process cannot access the file because it is being used by another process c#

I have seen several post for this problem .I have implemented all suggestion like using flush() , close() method on streamwriter and connection Object,use GC.Collect() to force cleanup,, use using{} to autodispose
I am Doing Simple Get Operation from DB and write to text file ..here is my Code
public void WriteToFile(string ProductName)
{
//Already Got Data from DB and stored in "ProductName"
//saving in File
if (!File.Exists(path11))
{
File.Create(path11);
StreamWriter tw = new StreamWriter(path11);
tw.WriteLine(ProductName+"#"+DateTime.Now.ToString());
tw.Flush();
tw.Close();
}
else if (File.Exists(path11))
{
StreamWriter tw = new StreamWriter(path11, true);
tw.WriteLine(ProductName + "#" + DateTime.Now.ToString());
tw.Flush();
tw.Close();
}
GC.Collect();
}
Another suggestion I Got is to lock the object ..But I cannot implement it ..
Any suggestion would be Helpful
File.Create creates the file and returns an open stream. You don't really need all that logic. Just use new StreamWriter(path11, true) which will create the file if it doesn't exist and append to it if it does. Also using is helpful:
public void WriteToFile(string ProductName)
{
//Get Data from DB and stored in "ProductName"
using (var tw = new StreamWriter(path11, true))
{
tw.WriteLine(ProductName+"#"+DateTime.Now.ToString());
}
}
FileCreate returns a stream which you should use to instantiate StreamWriter:
var file = File.Create(path11);
StreamWriter tw = new StreamWriter(file);
And you should use using blocks to make sure your stream and file is closed when you're finished writing.

Writing data from textbox into text file

Here is the code im using to write and read from text file.
StreamWriter sw1 = new StreamWriter("DataNames.txt");
sw1.WriteLine(textBox1.Text);
sw1.Close();
StreamWriter sw2 = new StreamWriter("DataNumbers.txt");
sw2.WriteLine(textBox2.Text);
sw2.Close();
FileInfo file1 = new FileInfo("DataNames.txt");
StreamReader sr1 = file1.OpenText();
while (!sr1.EndOfStream)
{
listBox1.Items.Add(sr1.ReadLine());
}
FileInfo file2 = new FileInfo("DataNumbers.txt");
StreamReader sr2 = file2.OpenText();
while (!sr2.EndOfStream)
{
listBox2.Items.Add(sr2.ReadLine());
}
The thing is that when I click my button to save data from my textboxes to my text files an error appears that says "The process cannot access the file 'C:\xxxx\xxxxxx\xxxxx\xxxx\xxxxx\xxxxx.txt' because it is being used by another process."
Can anyone tell me why I have this error and maybe help me fix it
Try added a using statment around your streams to make sure they are Disposed otherwise the file is still locked to the stream
Example:
//Write
using (StreamWriter sw1 = new StreamWriter("DataNames.txt"))
{
sw1.WriteLine(textBox1.Text);
}
using (StreamWriter sw2 = new StreamWriter("DataNumbers.txt"))
{
sw2.WriteLine(textBox2.Text);
}
// Read
foreach (var line in File.ReadAllLines("DataNames.txt"))
{
listBox1.Items.Add(line);
}
foreach (var line in File.ReadAllLines("DataNumbers.txt"))
{
listBox2.Items.Add(line);
}
It appears you do not close the file after you read it. After you call FileInfo.OpenText you get a StreamReader which has to be closed, either via Close method, or even better, with a using statement.
But there are already methods that do all that for you, have a look at File.WriteAllText,
File.AppendAllText and File.ReadAllLines methods.
You need to Close the StreamReader object once you do not need it any more. This should fix this issue.
I.e.
StreamReader sr1 = file1.OpenText();
try {
while (!sr1.EndOfStream)
{
listBox1.Items.Add(sr1.ReadLine());
}
}
finally {
sr1.Close();
}
FileInfo file2 = new FileInfo("DataNumbers.txt");
StreamReader sr2 = file2.OpenText();
try {
while (!sr2.EndOfStream)
{
listBox2.Items.Add(sr2.ReadLine());
}
}
finally {
sr2.Close();
}
You have opened files but not closed.
StreamReader sr1 = file1.OpenText();
StreamReader sr2 = file2.OpenText();
Your problem occurs, because you are not closing the stream readers.
A safer way of using external resources (the files in this case) is to embed their use in a using statement. The using statement automatically closes the resource at the end of the statement block or if the statement block if left in another way. This could be a return statement or an exception, for instance. It is guaranteed that the resource will be closed, even after an exception occurs.
You can apply the using statement on any object which implements the IDisposable interface.
// Writing to the files
using (var sw1 = new StreamWriter("DataNames.txt")) {
sw1.WriteLine(textBox1.Text);
}
using(var sw2 = new StreamWriter("DataNumbers.txt")) {
sw2.WriteLine(textBox2.Text);
}
// Reading from the files
FileInfo file1 = new FileInfo("DataNames.txt");
using (StreamReader sr1 = file1.OpenText()) {
while (!sr1.EndOfStream) {
listBox1.Items.Add(sr1.ReadLine());
}
}
FileInfo file2 = new FileInfo("DataNumbers.txt");
using (StreamReader sr2 = file2.OpenText()) {
while (!sr2.EndOfStream)
{
listBox2.Items.Add(sr2.ReadLine());
}
}
However, you can simplify the reading part like this
// Reading from the files
listBox1.Items.AddRange(File.ReadAllLines("DataNames.txt"));
listBox2.Items.AddRange(File.ReadAllLines("DataNumbers.txt"));
I've seen this behavior before - usually there's another process open that's blocking the file access. Do you have multiple development servers open in your taskbar? (Strange, yes, but I've seen it happen)

Delete file using File.Delete and then using a Streamwriter to create the same file?

public void LoadRealmlist()
{
try
{
File.Delete(Properties.Settings.Default.WoWFolderLocation +
"Data/realmlist.wtf");
StreamWriter TheWriter =
new StreamWriter(Properties.Settings.Default.WoWFolderLocation +
"Data/realmlist.wtf");
TheWriter.WriteLine("this is my test string");
TheWriter.Close();
}
catch (Exception)
{
}
}
Will my method properly delete a file, then create one with "realmlist.wtf" as the name and then write a line to it?
I'm kind of confused because I can't see the line where it actually creates the file again. Or does the act of creating a StreamWriter automatically create a file?
The Stream Writer will create the file if it doesn't exist.
It will create it in the constructor, so when the StreamWriter is instantiated.
You know, if you pass a FileStream instance to the StreamWriter constructor, it can be set to simply overwrite the file. Just pass it along with the constructor.
http://msdn.microsoft.com/en-us/library/system.io.filestream.filestream.aspx
Example:
try
{
using (FileStream fs = new FileStream(filename, FileMode.Create))
{
//FileMode.Create will make sure that if the file allready exists,
//it is deleted and a new one create. If not, it is created normally.
using (StreamWriter sw = new StreamWriter(fs))
{
//whatever you wanna do.
}
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
Also, with this you won't need to use the .Close method. The using() function does that for you.
Try System.IO.File.CreateText.

Categories