Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an Empty FileInfo Object in C# without an existing file

I added an alternative code-path using an input string rather than reading from a file. I would require an empty FileInfo object as I have many instances which access the Name, Length and Extension property.

Ideally I am looking for something like

FileInfo _fileinfo = new FileInfo(File.Empty);

However there is only one FileInfo constructor, which appears to require a valid file. Any solution to creating an empty-initialized FileInfo object, which does not require the creation of an empty dummy file?

like image 910
Lorenz Lo Sauer Avatar asked Sep 20 '12 17:09

Lorenz Lo Sauer


1 Answers

I just came across a similar problem. What do you think about starting with:

FileInfo _fileinfo = null;

After that, you could just do:

_fileinfo = new FileInfo(<string of file with path>);

You would then have an object that you could pass as parameter to your methods. Don't foget to check if your object is null before you try to get the values for .Name and so on

if(null != _fileinfo)
{
  //some code
}
like image 55
Zhatyr Avatar answered Sep 21 '22 06:09

Zhatyr