473,383 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

C# FileStream Handling

Hi,

Expand|Select|Wrap|Line Numbers
  1. using (FileStream fs = File.Create(path))
  2. {
  3.                 Byte[] info = new UTF8Encoding(true).GetBytes("Text");
  4.                 // Add some information to the file.
  5.                 fs.Write(info, 0, info.Length);
  6.                 fs.Close(); // Is it mandaroty to call Close on stream
  7. }
My understanding is that in above piece of code, no need to fs.Close() since it will be handled when the scope of using block finishes.
Whether it is corret.
Nov 21 '07 #1
4 1601
CyberSoftHari
487 Expert 256MB
That is closing the block not the file stream. You have to use it mandaroty.
Nov 21 '07 #2
r035198x
13,262 8TB
Hi,

Expand|Select|Wrap|Line Numbers
  1. using (FileStream fs = File.Create(path))
  2. {
  3.                 Byte[] info = new UTF8Encoding(true).GetBytes("Text");
  4.                 // Add some information to the file.
  5.                 fs.Write(info, 0, info.Length);
  6.                 fs.Close(); // Is it mandaroty to call Close on stream
  7. }
My understanding is that in above piece of code, no need to fs.Close() since it will be handled when the scope of using block finishes.
Whether it is corret.
That using decomposes to code with a try/finally thing with IDisposable.Dispose() called in the finally part.
If Microsoft have implemented IDisposable correctly for FileStream (I think they have) then thre's no need for that Close there.
Nov 21 '07 #3
Plater
7,872 Expert 4TB
I would recomend calling .Close() regardless, it's good practice.
Also pay attention to the tooltip on the .Close() method, filestream says it closes the filehandle and stream, BUT some of the other stream objects do NOT close underlying streams when you call .Close(), so it creates memory leaks.
Nov 21 '07 #4
r035198x
13,262 8TB
Like I said, "using" calls Dispose() which is "stronger" (in terms of clean up) than Close(). Putting Close inside the using block is just redundant and shows lack of understanding of the using block itself while defeating the purpose of using it.
Nov 21 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
4
by: khan | last post by:
Hi guys I see blue line underneat the word FileStream in the following code in line 2. Can any one help me to find out what am I missing Imports System.IO Imports System.Windows.Forms Public...
2
by: ewingate | last post by:
The following code which is supposed to dynamically create files with incrementing names is throwing an exception due to the inclusion of the DateTime.Now component of the sReportSave string: ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.