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?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))
rn5a@.rediffmail.com wrote:
> 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?
Hi,
rn5a@.rediffmail.com wrote:
> 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
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.
Laurent Bugnion [MVP] wrote:
> Hi,
> rn5a@.rediffmail.com wrote:
> 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
Hi,
rn5a@.rediffmail.com wrote:
> 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
--
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
Saturday, March 24, 2012
Populate ListBox With Directories & Files
Labels:
asp,
directories,
directory,
files,
listbox,
listboxto,
net,
particular,
populate,
thatexist
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment