Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

.NET Forum


You are currently viewing our .NET Forum as a guest. Please register to participate.
Login



Reply
list of files in directory
Old 08-16-2006, 12:13 PM list of files in directory
Experienced Talker

Posts: 48
Trades: 0
hi Guys,

i am using vs2005

i want to make a page that will list the files (and maybe some of there attributes) in a given folder. and if u click on one it will open that file.

i know this has something to do with the directory class, but am unsure of how to go about this in VS2005.
__________________
regards,

Pauly.
The true sign of intelligence is not knowledge but imagination. (Albert Einstein)
Be who you are and say what you feel,
because those who mind don't matter and those who matter don't mind. (Dr. Seuss
)
paulwebmaster is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-17-2006, 05:42 AM Re: list of files in directory
Minaki's Avatar
Defies a Status

Posts: 1,626
Location: Guildford, UK
Trades: 0
Use the System.IO namespace, then:
Code:
DirectoryInfo MyDirectoryInfo = new DirectoryInfo(Path);
Where Path is a string containing the path of the folder you want to list the files from.
MyDirectoryInfo.GetFiles() will now return a list of files in that directory. See:
DirectoryInfo.GetFiles Method (System.IO)
It returns an array of FileInfo objects so can be databound to any of the data controls, such as a repeater or DataGrid. ie:
Code:
dgFiles.DataSource = MyDirectoryInfo.GetFiles();
dgFiles.DataBind();
Here's a list of FileInfo members that you can use as columns in your DataGrid to display the file info:
FileInfo Members (System.IO)

Here's an example DataGrid:
Code:
    <asp:DataGrid ID="dgFiles" runat="server" AutoGenerateColumns="false" Width="100%">
     <Columns>
      <asp:TemplateColumn HeaderText="File Name">
       <ItemTemplate>
        <a href="<%# ((FileInfo)Container.DataItem).Name %>"><%# ((FileInfo)Container.DataItem).Name %></a>
       </ItemTemplate>
      </asp:TemplateColumn>
      <asp:BoundColumn DataField="CreationTime" HeaderText="Created" ItemStyle-Width="150px" />
      <asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Modified" ItemStyle-Width="150px" />
      <asp:BoundColumn DataField="Length" HeaderText="File Size" ItemStyle-Width="100px" />
      <asp:ButtonColumn Text="Delete" HeaderText="Delete" CommandName="Delete" ItemStyle-Width="100px" />
     </Columns>
    </asp:DataGrid>
Of course, you could use any of the data controls, such as a repeater.
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Minaki is offline
Reply With Quote
View Public Profile Visit Minaki's homepage!
 
Old 08-17-2006, 08:12 AM Re: list of files in directory
Experienced Talker

Posts: 48
Trades: 0
thanks Minaki,

but this is still all a bit new to me.

i got as far as importing the namespace, then got lost.

DirectoryInfo MyDirectoryInfo = new DirectoryInfo(c:\);?????

do u know of any good tutorils on this subject???

or do u have all the code?

thanks again,
__________________
regards,

Pauly.
The true sign of intelligence is not knowledge but imagination. (Albert Einstein)
Be who you are and say what you feel,
because those who mind don't matter and those who matter don't mind. (Dr. Seuss
)
paulwebmaster is offline
Reply With Quote
View Public Profile
 
Old 08-17-2006, 09:47 AM Re: list of files in directory
Minaki's Avatar
Defies a Status

Posts: 1,626
Location: Guildford, UK
Trades: 0
Try going through the quickstart tutorials at Home : The Official Microsoft ASP.NET 2.0 Site : English
Once you get an understanding of the architecture of ASP.NET, the code above should become clear. The code I've got above is practicly complete. You just need to copy it into an existing page.

DirectoryInfo MyDirectoryInfo = new DirectoryInfo(c:\);
C:\ needs to be in quotes, since it's a string:
DirectoryInfo MyDirectoryInfo = new DirectoryInfo("c:\\");
(Also, in C#, \ is an escape character so you need to escape it with itself, thus the double \\)
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Minaki is offline
Reply With Quote
View Public Profile Visit Minaki's homepage!
 
Old 08-17-2006, 11:00 AM Re: list of files in directory
Experienced Talker

Posts: 48
Trades: 0
i wanna do in VB....
__________________
regards,

Pauly.
The true sign of intelligence is not knowledge but imagination. (Albert Einstein)
Be who you are and say what you feel,
because those who mind don't matter and those who matter don't mind. (Dr. Seuss
)
paulwebmaster is offline
Reply With Quote
View Public Profile
 
Old 08-17-2006, 01:26 PM Re: list of files in directory
Minaki's Avatar
Defies a Status

Posts: 1,626
Location: Guildford, UK
Trades: 0
It's practicly the same except:

DirectoryInfo MyDirectoryInfo = new DirectoryInfo(Path);
becomes:
Dim MyDirectoryInfo as new DirectoryInfo(Path)


dgFiles.DataSource = MyDirectoryInfo.GetFiles();
dgFiles.DataBind();
becomes:

dgFiles.DataSource = MyDirectoryInfo.GetFiles()
dgFiles.DataBind()

you can forget about the \\ in the string since VB doesn't use backslash as an escape character. Just use a single one as normal. i.e.
... new DirectoryInfo("c:\")

It's a good idea to know the basic syntax rules of C# as well as VB, even if you never code in C#. Almost everything in C# can be translated effortlessly to VB, and you'll find just over half of all examples or .NET on the web are likely to be C#.




Here's a good cheat sheet:
C# and VB.NET Comparison Cheat Sheet: ASP Alliance
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Minaki is offline
Reply With Quote
View Public Profile Visit Minaki's homepage!
 
Old 08-18-2006, 08:15 AM Re: list of files in directory
Experienced Talker

Posts: 48
Trades: 0
dgfiles says "Declaration expected"
i tried
dim dgfiles as new datagrid
but this didnt help


<%
@PageLanguage="VB"AutoEventWireup="false"CodeFile="browser.aspx.vb"Inherits="browser" %>
<%
@ImportNamespace="System.IO" %>
<scriptlanguage="VB"runat="server">
Dim MyDirectoryInfo AsNew DirectoryInfo("C:\_websites\intranet\minutes")

dgFiles.DataSource = MyDirectoryInfo.GetFiles()
dgFiles.DataBind()


</script>

with

<asp:DataGridID="dgFiles"runat="server"AutoGenerateColumns="false"Width="100%">
<Columns>
<asp:TemplateColumnHeaderText="File Name">
<ItemTemplate>
<ahref="<%# ((FileInfo)Container.DataItem).Name %>">
<%# ((FileInfo)Container.DataItem).Name %> </a>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumnDataField="CreationTime"HeaderText="Created"ItemStyle-Width="150px"/>
<asp:BoundColumnDataField="LastWriteTime"HeaderText="Last Modified"ItemStyle-Width="150px"/>
<asp:BoundColumnDataField="Length"HeaderText="File Size"ItemStyle-Width="100px"/>
<asp:ButtonColumnText="Delete"HeaderText="Delete"CommandName="Delete"ItemStyle-Width="100px"/>
</Columns>
</asp:DataGrid>
__________________
regards,

Pauly.
The true sign of intelligence is not knowledge but imagination. (Albert Einstein)
Be who you are and say what you feel,
because those who mind don't matter and those who matter don't mind. (Dr. Seuss
)
paulwebmaster is offline
Reply With Quote
View Public Profile
 
Old 08-18-2006, 10:06 AM Re: list of files in directory
Minaki's Avatar
Defies a Status

Posts: 1,626
Location: Guildford, UK
Trades: 0
It needs to be in the Page_Load event:
Code:
Sub Page_Load
  Dim MyDirectoryInfo AsNew DirectoryInfo("C:\_websites\intranet\minutes")
 
  dgFiles.DataSource = MyDirectoryInfo.GetFiles()
  dgFiles.DataBind()
End Sub

__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Minaki is offline
Reply With Quote
View Public Profile Visit Minaki's homepage!
 
Old 08-18-2006, 10:52 AM Re: list of files in directory
Experienced Talker

Posts: 48
Trades: 0
ok, that worked.

but now:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30108: 'FileInfo' is a type and cannot be used as an expression.

Source Error:

Line 18: <asp:TemplateColumn HeaderText="File Name">
Line 19: <ItemTemplate>
Line 20: <a href="<%# ((FileInfo)Container.DataItem).Name %>"><%# ((FileInfo)Container.DataItem).Name %> </a>
Line 21: </ItemTemplate>


and then i suppose i want to change it to look something like(without the port number (after testing)):

<ahref="http://localhost:3397/intranet/minutes/<%# ((FileInfo)Container.DataItem).Name %>"><%# ((FileInfo)Container.DataItem).Name %> </a>
__________________
regards,

Pauly.
The true sign of intelligence is not knowledge but imagination. (Albert Einstein)
Be who you are and say what you feel,
because those who mind don't matter and those who matter don't mind. (Dr. Seuss
)

Last edited by paulwebmaster; 08-18-2006 at 11:03 AM..
paulwebmaster is offline
Reply With Quote
View Public Profile
 
Old 08-18-2006, 12:05 PM Re: list of files in directory
Minaki's Avatar
Defies a Status

Posts: 1,626
Location: Guildford, UK
Trades: 0
Hmm sorry forgot to conver that bit of code from C#...
I can't remember how to do typecasting in VB, I think it's:
<%# (Container.DataItem As FileInfo).Name %>
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Minaki is offline
Reply With Quote
View Public Profile Visit Minaki's homepage!
 
Old 08-18-2006, 12:31 PM Re: list of files in directory
Experienced Talker

Posts: 48
Trades: 0
sorry Minaki

now is saying:


Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30456: 'Name' is not a member of 'String'.

Source Error:

Line 18: <asp:TemplateColumn HeaderText="File Name">
Line 19: <ItemTemplate>
Line 20: <a href="http://localhost:3397/intranet/minutes/<%# (Container.DataItem As FileInfo).Name %>"><%# (Container.DataItem As FileInfo).Name %></a>

Line 21: </ItemTemplate>Line 22: </asp:TemplateColumn>


im not sure im writing it properly

<asp:TemplateColumnHeaderText="File Name">
<ItemTemplate>
<a href="http://localhost:3397/intranet/minutes/<%# (Container.DataItem As FileInfo).Name %>">whatever</a>
</ItemTemplate>
</asp:TemplateColumn>
__________________
regards,

Pauly.
The true sign of intelligence is not knowledge but imagination. (Albert Einstein)
Be who you are and say what you feel,
because those who mind don't matter and those who matter don't mind. (Dr. Seuss
)

Last edited by paulwebmaster; 08-18-2006 at 12:35 PM..
paulwebmaster is offline
Reply With Quote
View Public Profile
 
Old 08-24-2006, 07:59 AM Re: list of files in directory
saadatshah's Avatar
Extreme Talker

Posts: 215
Name: Syed Saadat Ali
Location: Lahore, Pakistan
Trades: 0
Go for some basic tutorials on .NET. Try W3Schools Online Web Tutorials
__________________
- -- --- ---- ----- ------ ------- ---------------
If you have knowledge, let others light their candles in it.
saadatshah is offline
Reply With Quote
View Public Profile Visit saadatshah's homepage!
 
Old 08-29-2006, 01:23 PM Re: list of files in directory
Experienced Talker

Posts: 48
Trades: 0
i managed to find this article which sorted me out:

ASP.NET.4GuysFromRolla.com: Displaying the Files in a Directory using a DataGrid
__________________
regards,

Pauly.
The true sign of intelligence is not knowledge but imagination. (Albert Einstein)
Be who you are and say what you feel,
because those who mind don't matter and those who matter don't mind. (Dr. Seuss
)
paulwebmaster is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to list of files in directory
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.45914 seconds with 12 queries