473,698 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FileInfo can't use in using

Can't use FileInfo in using statement?

I have the following statement:

using (FileInfo fInfo = new FileInfo(fromIm agePath + "\\" +
(string)dr["originalFileNa me"]))

and I get the following error:

'System.IO.File Info': type used in a using statement must be
implicitly convertible to 'System.IDispos able'

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 14538
tshad <ts***@dslextre me.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.co m>
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.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
tshad <ts***@dslextre me.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.co m>
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***@dslextre me.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.co m>
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.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
tshad <ts***@dslextre me.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(fromIm agePath + "\\" +
(string)dr["originalFileNa me"]))

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.co m>
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***@dslextre me.comwrote:
So the object "fInfo" in

using (FileInfo fInfo = new FileInfo(fromIm agePath + "\\" +
(string)dr["originalFileNa me"]))

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***@dslextre me.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(fromIm agePath + "\\" +
(string)dr["originalFileNa me"]))

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.co m>
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
17020
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 company distribution of data. When I try to call the FileInfo.MoveTo() method, I receive the following error: System.UnauthorizedAccessException: Access to the path is denied.
1
7840
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 supported. Can I use URI paths when using FileInfo class?? I loaded xml into XmlDocument object using URI paths but couldnt use it in FileInfo class. Any ideas??
2
2510
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 the Dll's and all the Txt's in a folder, if appears I need to do something like the following: ArrayList InterestingFiles = new ArrayList(); DirectoryInfo di = new DirectoryInfo(@"C:\My Folder");
5
6535
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 attempted to edit the value of the System.IO.FileInfo properties. Is there an existing UITypeEditor that will do this type of thing, or will I need to create my own Thanks Lance
1
2585
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? My code is creating a file correctly, but the CreationTime is (sometimes) set to the CreationTime of an old file that has previously been removed. The line of code creating the file is _LogFileWriter = newFile.CreateText()
1
1720
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
2165
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 use the DirectoryInfo class?
2
1431
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 on C# if I am getting _folderName and by using FileInfo instance ?
6
159
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: 'System.IO.FileInfo': type used in a using statement must be
0
9166
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.