articleList.DataSource =
dirInfo.GetFiles("*.rpt").ToString().Replace(".rpt", "");
articleList.DataBind()
I think that works.
-Darren
"Lyners" <Lyn...@discussions.microsoft.com> wrote in message
news:EA2DD210-23E5-47CD...@microsoft.com...
>I have code that retrieves all of the file names within a directory. After
> retriving them, I display the information in a datagrid. What I would like
> to
> do is add an extra output column on the directory info that would contain
> the
> file name without the extension. Can I add an attribute or an entry to
> accomplish this? If so, how, if not, is there anything I can do to do
> this.
> Here is my code that i am using;
>
> Dim dirInfo as New DirectoryInfo(Server.MapPath("/Admin/report"))
>
> articleList.DataSource = dirInfo.GetFiles("*.rpt")
> articleList.DataBind()
>
> It displays reportname.rpt, I would like reportname minus the extension.
>
> Thanks
<asp:datagrid id="articleList" runat="server" HeaderStyle-Font-Bold="True"
HeaderStyle-Font-Size="15pt"
HeaderStyle-ForeColor="White" HeaderStyle-BackColor="Navy"
AlternatingItemStyle-BackColor="#eeeeee"
AutoGenerateColumns="False" Font-Name="Verdana">
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name"
DataNavigateUrlFormatString="_displaycr.aspx?reportID={0}"
DataTextField="Name" HeaderText="File Name" target="_report" />
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Date Created"
ItemStyle-HorizontalAlign="Center"
DataFormatString="{0:d}" />
</Columns>
</asp:datagrid>
I tried what you came up with here and got an error:
A field or property with the name 'Name' was not found on the selected
datasource.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details: System.Web.HttpException: A field or property with the
name 'Name' was not found on the selected datasource.
Source Error:
Line 5:
Line 6: articleList.DataSource =
dirInfo.GetFiles("*.rpt").ToString().Replace(".rpt", "")
Line 7: articleList.DataBind()
Line 8: End Sub
Line 9: </script>
Can I add a column to the getfile that is like name2 that would have the
result of the file name minus the extension?
The directory contents class is defined as follows -
public class DirectoryContents
{
private string _Name;
private DateTime _LastWriteDate;
public string Name
{
get { return this._Name; }
set { this._Name = value; }
}
public DateTime LastWriteDate
{
get { return this._LastWriteDate; }
set { this._LastWriteDate = value; }
}
// constructor
public DirectoryContents(string ItemName, DateTime
WriteDate)
{
this.Name = ItemName;
this.LastWriteDate = WriteDate;
}
}
Then I have a function that returns an array list (you can bind directly to
an ArrayList). It does the following. -
// note, static is only because I was calling this from a seperate code file
public static ArrayList GetFiles(string Path)
{
// lets us use Server.MapPath
HttpContext context = HttpContext.Current;
// get list of all images in the current directory
DirectoryInfo ParentDirectory = new
DirectoryInfo(context.Server.MapPath(Path));
// Arraylist to hold the files
ArrayList DirectoryContents = new ArrayList();
// loop through all the files
foreach (FileInfo ReportFile in ParentDirectory.GetFiles())
{
// only return the reports (rpt extension)
if (ImageFile.Extension.ToLower() == ".rpt")
{
// create a new instance of the
DirectoryContents class
DirectoryContents.Add(new
DirectoryContents(ReportFile.Name.ToString().Replace(".rpt", ""),
ReportFile.LastWriteTime));
}
}
// return the data
return DirectoryContents;
}
I'll work on converting this to VB.Net syntax, but you can see what I am
basically doing. The class holds the data for each file. You can also add
more fields depending on what you need (file name, file path, create date,
etc).
Then I call a function called GetFiles and pass in the path to the folder I
want to get files for (you could pass in the absolute location, I pass in a
relative location then I map the path). Basically what this function does
is it creates an arraylist (which will be returned later), loops through all
the files in the directory, if the file is a report, we add a new instance
of our DirectoryContents class to the arraylist. When we are done we return
the arraylist to wherever (could return to DataGrid.DataSource).
When you bind to the ArrayList to the datagrid, you reference the DataItems
by the public name in the class. So for example, a bound column that was to
display the last write date would reference the data field as LastWriteDate.
You would reference the file name as Name, etc.
Hope this helps. In my app I was listing out all image files, and I found
the approach I used to be quick and efficient way of grabbing the data.
-Darren
"Lyners" <Lyn...@discussions.microsoft.com> wrote in message
news:374B45F8-6901-46D1...@microsoft.com...
articleList.DataSource = GetFiles("path")
articleList.DataBind()
Still workin on the vb version of my code, i'm a bit rusty at vb so it
might take me a day or so
-Darren
Public Class DirectoryContents
Private _Name As String
Private _nameWithoutExtension As String
Private _lastWriteDate As DateTime
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property NameWithoutExtension() As String
Get
Return _nameWithoutExtension
End Get
Set(ByVal Value As String)
_nameWithoutExtension = Value
End Set
End Property
Public Property LastWriteDate() As DateTime
Get
Return _lastWriteDate
End Get
Set(ByVal Value As DateTime)
_lastWriteDate = Value
End Set
End Property
Public Sub New(ByVal ItemName As String, ByVal WriteDate As DateTime)
Name = ItemName
LastWriteDate = WriteDate
NameWithoutExtension = ItemName.Replace(".rpt", "")
End Sub
End Class
In the behind code;
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
articleList.DataSource = getFiles("path")
articleList.DataBind()
End Sub
Private Function getFiles(ByVal path As String) As ArrayList
Dim dirInfo As New DirectoryInfo(Server.MapPath(path))
Dim DirectoryContents As ArrayList = New ArrayList
Dim ReportFile As FileInfo
For Each ReportFile In dirInfo.GetFiles("*.rpt")
DirectoryContents.Add(New
DirectoryContents(ReportFile.Name.ToString, ReportFile.LastWriteTime))
Next
getFiles = DirectoryContents
End Function
Thanks Darren, works great!
Happy .NETing!
Darren Kopp
"Lyners" <Lyn...@discussions.microsoft.com> wrote in message
news:CFDD5233-AB0F-43C6...@microsoft.com...