472,985 Members | 3,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,985 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 14345
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jman | last post by:
I have an urgent question. I have written a .Net C# windows services that has only one key purpose: to move files from the machine that is running the service to a network download server for...
1
by: RK | last post by:
private const string appdir = "http://localhost/myApp"; string strPath = appdir + "/xml/test.log"; FileInfo f = new FileInfo(strPath); the above code throwing error, like URI formats are not...
2
by: John Bowman | last post by:
Hi All, ..NET 1.1... I'm wondering if there is any approach more convenient to get a list of FileInfo objects than the following. For example, if I wanted to get 1 list of all the Exe's and all...
5
by: Lance | last post by:
I want to expose properties of a class to a user via a PropertyGrid class. Some of the properties are of type System.IO.FileInfo. Ideally, an OpenFileDialog window would appear when the user...
1
by: Tim Failes | last post by:
This seems a trival question, but I cannot get it to work properly... Essentially my question is, how can I create a text file, and guarantee it is given the current date/time as the Creation Time?...
1
by: Michael A. Covington | last post by:
I just saw something like this in someone else's code and am curious... string fname; .... store a filename in fn ... FileInfo fi = new FileInfo(fname); string name = fi.Name;
3
by: rn5a | last post by:
To work with files, when should one use the File class & when should one use the FileInfo class? Similarly, to work with directories, when should one use the Directory class & when should one...
2
by: csharpula csharp | last post by:
Hello, I create a file using FileInfo by : NewFile = new FileInfo(FileName); and I got a parameter which tells me in which folder I need to place it on my filesystem. How can I implement it...
6
by: tshad | last post by:
Can't use FileInfo in using statement? I have the following statement: using (FileInfo fInfo = new FileInfo(fromImagePath + "\\" + (string)dr)) and I get the following error: ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.