FileSystemInfo, DirectoryInfo, FileInfo

  • Thread starter Thread starter Massimiliano Alberti
  • Start date Start date
M

Massimiliano Alberti

Now... I have a file "c:\test". I don't know if it's a file ora directory. I
can't instanciate FileSystemInfo because it's an abstract class.
What must I do? Instanciate a DI, check if it's a Directory and if it isn't
instanciate a FI? Is there a better method?

OR

I could parse the filename, extract the parent directory info (c:\), and
then, knowing it's a directory, create a DirectoryInfo for the parent, and
then use the GetFileSystemInfos to retrieve a FileSystemInfo (that in truth
is a DI or a FI casted down).

--- bye
 
Massimiliano said:
Now... I have a file "c:\test". I don't know if it's a file ora directory. I
can't instanciate FileSystemInfo because it's an abstract class.
What must I do? Instanciate a DI, check if it's a Directory and if it isn't
instanciate a FI? Is there a better method?

OR

I could parse the filename, extract the parent directory info (c:\), and
then, knowing it's a directory, create a DirectoryInfo for the parent, and
then use the GetFileSystemInfos to retrieve a FileSystemInfo (that in truth
is a DI or a FI casted down).

I recommend You to call:

string myPath=@"c:\test";
bool isMyPathDir=Directory.Exists(myPath);
bool isMyPathFile=File.Exists(myPath);

FileSystemInfo myPathInfo=null;
if( isMyPathDir ) {
myPathInfo=new DirectoryInfo(myPath);
}
else if( isMyPathFile ) {
myPathInfo=new FileInfo(myPath);
}

Regards

Marcin
 
Massimiliano Alberti said:
Now... I have a file "c:\test". I don't know if it's a file ora directory. I
can't instanciate FileSystemInfo because it's an abstract class.
What must I do? Instanciate a DI, check if it's a Directory and if it isn't
instanciate a FI? Is there a better method?

Just instantiate a FileInfo - it doesn't matter if it happens to be a
directory.
 
I recommend You to call:
string myPath=@"c:\test";
bool isMyPathDir=Directory.Exists(myPath);
bool isMyPathFile=File.Exists(myPath);
Wow! Thanks! I didn't know of this trick :-)

--- bye
 
Back
Top