Given a System.IO.File get the directory

  • Thread starter Thread starter SoxFan44
  • Start date Start date
S

SoxFan44

Hi,
If I have a File("c:\test\subdir\file1.html"), what's the best way to get
the directory of this file, ie 'c:\test\subdir'? Thanks!
 
MessageBox.Show(System.IO.Path.GetDirectoryName("c:\\test\\subdir\\file1.html").ToString());
 
System.IO.FileInfo fi = new
System.IO.FileInfo(@"c:\test\subdir\file1.html");
Response.Write(fi.DirectoryName);
 
i think, it is long way, is it?
Deepak said:
System.IO.FileInfo fi = new
System.IO.FileInfo(@"c:\test\subdir\file1.html");
Response.Write(fi.DirectoryName);
 
Hi,

there is no way that you can have a File(....) , File does not have a
constructor as all the methods are static

if you have a File.Open( ... ) you will get a FileStream which has a Name
property. Then you can use System.IO.Path.GetDirectoryName to get the name
of the directory
 
Back
Top