FileInfo can't use in using

T

tshad

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
 
J

Jon Skeet [C# MVP]

tshad said:
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.
 
T

tshad

Jon Skeet said:
No, because it doesn't implement IDisposable.


Do you see a Close method defined anywhere on FileInfo? What would you
expect it to do?


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
 
J

Jon Skeet [C# MVP]

tshad said:
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.
 
T

tshad

Jon Skeet said:
When there are unmanaged resources involved, such as open files.


No. FileInfo reads file system information when it needs to, but it
doesn't hold any unmanaged resources open.


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
 
J

Jon Skeet [C# MVP]

tshad said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top