Populate ListBox With Directories & Files

R

rn5a

I have a ListBox which should list all the files & directories that
exist in a particular directory. The problem is I can get the ListBox
to list either all the files or all the directories but not the 2 of
them together. This is what I tried:

Sub Page_Load(.....)
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath(MyDir))

'get all the directories existing in MyDir
lstFilesDirs.DataSource = dInfo.GetDirectories

'get all the files existing in MyDir
lstFilesDirs.DataSource = dInfo.GetFiles

lstFilesDirs.DataBind()
End Sub

<form runat="server">
<asp:ListBox ID="lstFilesDirs" runat="server"/>
</form>

In the above code, since the GetFiles method has been issued after the
GetDirectories method, the ListBox lists only the files existing in the
directory named MyDir. On the other hand, had the GetFiles method been
issued after the GetDirectories method, then the ListBox would have
listed only the directories existing in the directory MyDir. So the
ListBox lists either all the files or all the directories existing in
the directory named MyDir.

How do I list all the directories as well as all the files existing in
the directory MyDir in the ListBox?
 
R

rn5a

Well, this can be done using the GetFileSystemInfos method of the
DirectoryInfo object. Here's how you do it (just in case, if someone
encounters a similar problem):

Sub Page_Load(.....)
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath(MyDir))

'get all directories as well as files existing in MyDir
lstFilesDirs.DataSource = dInfo.GetFileSystemInfos
lstFilesDirs.DataBind()
End Sub

Even the GetFileSystemEntries method of the Directory object can be
used to get all the directories & files existing in a directory. The
difference between GetFileSystemInfos & GetFileSystemEntries is that
the latter, being a method of the Directory class, will get the
physical path of the files & directories as well along with the file
names & directory names respectively.

lstFilesDirs.DataSource =
Directory.GetFileSystemEntries(Server.MapPath(My Dir))
 
L

Laurent Bugnion [MVP]

Hi,

I have a ListBox which should list all the files & directories that
exist in a particular directory. The problem is I can get the ListBox
to list either all the files or all the directories but not the 2 of
them together. This is what I tried:

Sub Page_Load(.....)
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath(MyDir))

'get all the directories existing in MyDir
lstFilesDirs.DataSource = dInfo.GetDirectories

'get all the files existing in MyDir
lstFilesDirs.DataSource = dInfo.GetFiles

I saw you got the answer according to your later post, but just to
clarify, you overwrite the DataSource property with the list of files
after you gave it the list of directories. This is not cumulative.

Since both FileInfo and DirectoryInfo derive from FileSystemInfo, you
can merge both arrays returned by GetDirectories and GetFiles (but in
that case, it doesn't make sense, since the method GetFileSystemInfo
gives you everything you need as you found out already).

HTH,
Laurent
 
R

rn5a

Thanks, Laurent. It was indeed very kind of you to pinpoint those
points.
Since both FileInfo and DirectoryInfo derive from FileSystemInfo, you
can merge both arrays returned by GetDirectories and GetFiles

Laurent, could you please tell me how to merge the arrays returned by
GetDirectories & GetFiles methods? A small code snippet would be highly
appreciated.
that case, it doesn't make sense, since the method GetFileSystemInfo
gives you everything you need as you found out already).

I guess it was a typo; the method is GetFileSystemInfos & not
GetFileSystemInfo.

Thanks once again.

Hi,

I have a ListBox which should list all the files & directories that
exist in a particular directory. The problem is I can get the ListBox
to list either all the files or all the directories but not the 2 of
them together. This is what I tried:

Sub Page_Load(.....)
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath(MyDir))

'get all the directories existing in MyDir
lstFilesDirs.DataSource = dInfo.GetDirectories

'get all the files existing in MyDir
lstFilesDirs.DataSource = dInfo.GetFiles

I saw you got the answer according to your later post, but just to
clarify, you overwrite the DataSource property with the list of files
after you gave it the list of directories. This is not cumulative.

Since both FileInfo and DirectoryInfo derive from FileSystemInfo, you
can merge both arrays returned by GetDirectories and GetFiles (but in
that case, it doesn't make sense, since the method GetFileSystemInfo
gives you everything you need as you found out already).

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
L

Laurent Bugnion [MVP]

Hi,

Thanks, Laurent. It was indeed very kind of you to pinpoint those
points.


Laurent, could you please tell me how to merge the arrays returned by
GetDirectories & GetFiles methods? A small code snippet would be highly
appreciated.

Off the top of my head:

DirectoryInfo dir = new DirectoryInfo( "d:\\temp" );
FileInfo[] files = dir.GetFiles();
DirectoryInfo[] dirs = dir.GetDirectories();

List<FileSystemInfo> all = new List<FileSystemInfo>();
all.AddRange( files );
all.AddRange( dirs );

FileSystemInfo[] allFileSystemInfos = all.ToArray();

foreach ( FileSystemInfo fileSystemInfo in allFileSystemInfos )
{
Console.WriteLine( fileSystemInfo.Name );
}

I am sure there are more elegant ways to do that though.

I guess it was a typo; the method is GetFileSystemInfos & not
GetFileSystemInfo.

Yes, sorry!

Thanks once again.

Greetings,
Laurent
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top