Create an empty FileInfo object in C # without an existing file

I added an alternative path code using the input line, rather than reading from a file. I will need an empty FileInfo object as I have many instances that access the Name , Length and Extension properties.

Ideally, I'm looking for something like

 FileInfo _fileinfo = new FileInfo(File.Empty); 

However, there is only one FileInfo constructor that seems to require a valid file. Any solution for creating an empty initialized FileInfo object that does not require creating an empty dummy file?

+4
source share
1 answer

I ran into a similar problem. What do you think about starting with:

 FileInfo _fileinfo = null; 

After that, you can simply do:

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

Then you will have an object that you could pass as a parameter to your methods. Do not try to check if your object is null before trying to get values ​​for .Name, etc.

 if(null != _fileinfo) { //some code } 
+4
source

Source: https://habr.com/ru/post/1435336/


All Articles