Given a System.IO.File get the directory

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!
 
E

ergintiravoglu

MessageBox.Show(System.IO.Path.GetDirectoryName("c:\\test\\subdir\\file1.html").ToString());
 
D

Deepak Khanal

System.IO.FileInfo fi = new
System.IO.FileInfo(@"c:\test\subdir\file1.html");
Response.Write(fi.DirectoryName);
 
E

ergintiravoglu

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);
 
I

Ignacio Machin \( .NET/ C# MVP \)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top