471,348 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

FileInfo can't use in using

Can't use FileInfo in using statement?

I have the following statement:

using (FileInfo fInfo = new FileInfo(fromImagePath + "\\" +
(string)dr["originalFileName"]))

and I get the following error:

'System.IO.FileInfo': type used in a using statement must be
implicitly convertible to 'System.IDisposable'

I assume that this means I can't use it in my using statement.

So does this would mean that I need to explicitly close fInfo object:

fInfo.Close()

Or should I just let the garbage collector handle it?

Thanks,

Tom
Jun 1 '08 #1
6 14051
tshad <ts***@dslextreme.comwrote:
Can't use FileInfo in using statement?
No, because it doesn't implement IDisposable.
So does this would mean that I need to explicitly close fInfo object:

fInfo.Close()
Do you see a Close method defined anywhere on FileInfo? What would you
expect it to do?
Or should I just let the garbage collector handle it?
Yes. There are no unmanaged resource to release.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 1 '08 #2

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
tshad <ts***@dslextreme.comwrote:
>Can't use FileInfo in using statement?

No, because it doesn't implement IDisposable.
>So does this would mean that I need to explicitly close fInfo object:

fInfo.Close()

Do you see a Close method defined anywhere on FileInfo? What would you
expect it to do?
>Or should I just let the garbage collector handle it?

Yes. There are no unmanaged resource to release.
How would you know when to close the object?

Doesn't FileInfo create an object and therefore resources?

Then why doesn't it need to be closed - yet FileStreams need to be closed?

Thanks,

Tom
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Jun 1 '08 #3
tshad <ts***@dslextreme.comwrote:
Yes. There are no unmanaged resource to release.

How would you know when to close the object?
When there are unmanaged resources involved, such as open files.
Doesn't FileInfo create an object and therefore resources?
No. FileInfo reads file system information when it needs to, but it
doesn't hold any unmanaged resources open.
Then why doesn't it need to be closed - yet FileStreams need to be closed?
While a FileStream is open, it has an OS handle to the open file.
That's an unmanaged resource.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 1 '08 #4

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
tshad <ts***@dslextreme.comwrote:
Yes. There are no unmanaged resource to release.

How would you know when to close the object?

When there are unmanaged resources involved, such as open files.
>Doesn't FileInfo create an object and therefore resources?

No. FileInfo reads file system information when it needs to, but it
doesn't hold any unmanaged resources open.
>Then why doesn't it need to be closed - yet FileStreams need to be
closed?

While a FileStream is open, it has an OS handle to the open file.
That's an unmanaged resource.
So the object "fInfo" in

using (FileInfo fInfo = new FileInfo(fromImagePath + "\\" +
(string)dr["originalFileName"]))

is just an object and will be disposed of by the garbage collector after the
method is exited or goes out of scope?

Thanks,

Tom
>
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Jun 1 '08 #5
On Sun, 01 Jun 2008 00:18:51 -0700, tshad <ts***@dslextreme.comwrote:
So the object "fInfo" in

using (FileInfo fInfo = new FileInfo(fromImagePath + "\\" +
(string)dr["originalFileName"]))

is just an object and will be disposed of by the garbage collector after
the
method is exited or goes out of scope?
It will be collected (I prefer that term over "disposed" since the latter
has a specific meaning in .NET) according to the standard garbage
collector rule: when it is no longer reachable.

Depending on how you use the instance, this may well be when the variable
goes out of scope (exiting scope within the method, or the method itself,
for example).

Also keep in mind that it's not just FileInfo that's being collected. It
may contain other things that are also unreachable when the FileInfo
instance becomes unreachable and those will be collected as well. That's
just how the garbage collection works. The beauty of it is that you don't
need to worry about it.

The "dispose" paradigm exists in .NET primarily because of unmanaged
resources, and if an object doesn't implement IDisposable, then you don't
need to dispose it (and can't :) ).

Pete
Jun 1 '08 #6
tshad <ts***@dslextreme.comwrote:
While a FileStream is open, it has an OS handle to the open file.
That's an unmanaged resource.

So the object "fInfo" in

using (FileInfo fInfo = new FileInfo(fromImagePath + "\\" +
(string)dr["originalFileName"]))

is just an object and will be disposed of by the garbage collector after the
method is exited or goes out of scope?
It's just an object, and it will be garbage collected *at some point*
after it is used for the last time. That might be before the method
finishes (the garbage collector can be quite aggressive in determining
when something has been used for the last time) or a lot later (if the
garbage collector happens not to run for a while).

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 1 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by jman | last post: by
2 posts views Thread by John Bowman | last post: by
1 post views Thread by Michael A. Covington | last post: by
3 posts views Thread by rn5a | last post: by
2 posts views Thread by csharpula csharp | last post: by
6 posts views Thread by tshad | last post: by
1 post views Thread by Ronak mishra | last post: by

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.