473,734 Members | 2,511 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 14548
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
17022
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
7846
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
2512
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
6542
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
2592
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
1722
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
2168
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
1434
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
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
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
8186
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...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
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...
1
3261
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
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.