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:
Quote:
Originally Posted by
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:
Quote:
Originally Posted by
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.
Quote:
Originally Posted by
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.
Quote:
Originally Posted by
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:
Quote:
Originally Posted by
Hi,
>
rn5a@.rediffmail.com wrote:
Quote:
Originally Posted by
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
Hi,
rn5a@.rediffmail.com wrote:
Quote:
Originally Posted by
Thanks, Laurent. It was indeed very kind of you to pinpoint those
points.
>
Quote:
Originally Posted by
>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.
Off the top of my head:
DirectoryInfo dir = new DirectoryInfo( "d:\\temp" );
FileInfo[] files = dir.GetFiles();
DirectoryInfo[] dirs = dir.GetDirectories();
List<FileSystemInfoall = 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.
Quote:
Originally Posted by
Quote:
Originally Posted by
>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.
Yes, sorry!
Quote:
Originally Posted by
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
0 comments:
Post a Comment