473,408 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Directoryinfo addattribute

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
Nov 19 '05 #1
8 1879
Dim dirInfo as New DirectoryInfo(Server.MapPath("/Admin/report"))

articleList.DataSource =
dirInfo.GetFiles("*.rpt").ToString().Replace(".rpt ", "");
articleList.DataBind()

I think that works.

-Darren

"Lyners" <Ly****@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.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

Nov 19 '05 #2
Hi Darren,
I should have posted my small datagrid with this so that you could see that
I am using the name to link to the report in my datagrid. Here is my datagrid;

<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?repor tID={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?

"Darren Kopp" wrote:
Dim dirInfo as New DirectoryInfo(Server.MapPath("/Admin/report"))

articleList.DataSource =
dirInfo.GetFiles("*.rpt").ToString().Replace(".rpt ", "");
articleList.DataBind()

I think that works.

-Darren

"Lyners" <Ly****@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.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


Nov 19 '05 #3
ae
get the lenght - 3

"Lyners" wrote:
Hi Darren,
I should have posted my small datagrid with this so that you could see that
I am using the name to link to the report in my datagrid. Here is my datagrid;

<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?repor tID={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?

"Darren Kopp" wrote:
Dim dirInfo as New DirectoryInfo(Server.MapPath("/Admin/report"))

articleList.DataSource =
dirInfo.GetFiles("*.rpt").ToString().Replace(".rpt ", "");
articleList.DataBind()

I think that works.

-Darren

"Lyners" <Ly****@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.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


Nov 19 '05 #4
Hi ae, I figure I should do it in the HTML, but when I try it, I get an error
because it can't find the field named "left(name,lenght(name)-4)". Any
suggestions?

"ae" wrote:
get the lenght - 3

"Lyners" wrote:
Hi Darren,
I should have posted my small datagrid with this so that you could see that
I am using the name to link to the report in my datagrid. Here is my datagrid;

<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?repor tID={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?

"Darren Kopp" wrote:
Dim dirInfo as New DirectoryInfo(Server.MapPath("/Admin/report"))

articleList.DataSource =
dirInfo.GetFiles("*.rpt").ToString().Replace(".rpt ", "");
articleList.DataBind()

I think that works.

-Darren

"Lyners" <Ly****@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.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

Nov 19 '05 #5
Well, you are getting that error because those are not defined by that
function. That function returns an array of strings. Here is what i did
when i had a similar thing to do (I list out all the images in a folder).
Basically, there are 2 parts to it - the DirectoryConents class, and an
array list to hold the directory contents.

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().Repla ce(".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" <Ly****@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
Hi Darren,
I should have posted my small datagrid with this so that you could see
that
I am using the name to link to the report in my datagrid. Here is my
datagrid;

<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?repor tID={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?

"Darren Kopp" wrote:
Dim dirInfo as New DirectoryInfo(Server.MapPath("/Admin/report"))

articleList.DataSource =
dirInfo.GetFiles("*.rpt").ToString().Replace(".rpt ", "");
articleList.DataBind()

I think that works.

-Darren

"Lyners" <Ly****@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.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


Nov 19 '05 #6
Oh yea. You would bind the data grid like so...

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

Nov 19 '05 #7
Thanks Darren I got it. Here is the code in VB;

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!

"Darren Kopp" wrote:
Well, you are getting that error because those are not defined by that
function. That function returns an array of strings. Here is what i did
when i had a similar thing to do (I list out all the images in a folder).
Basically, there are 2 parts to it - the DirectoryConents class, and an
array list to hold the directory contents.

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().Repla ce(".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" <Ly****@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
Hi Darren,
I should have posted my small datagrid with this so that you could see
that
I am using the name to link to the report in my datagrid. Here is my
datagrid;

<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?repor tID={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?

"Darren Kopp" wrote:
Dim dirInfo as New DirectoryInfo(Server.MapPath("/Admin/report"))

articleList.DataSource =
dirInfo.GetFiles("*.rpt").ToString().Replace(".rpt ", "");
articleList.DataBind()

I think that works.

-Darren

"Lyners" <Ly****@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.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


Nov 19 '05 #8
Awesome, glad to hear that it's all working good. Best of luck.

Happy .NETing!
Darren Kopp

"Lyners" <Ly****@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
Thanks Darren I got it. Here is the code in VB;

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!

"Darren Kopp" wrote:
Well, you are getting that error because those are not defined by that
function. That function returns an array of strings. Here is what i did
when i had a similar thing to do (I list out all the images in a folder).
Basically, there are 2 parts to it - the DirectoryConents class, and an
array list to hold the directory contents.

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().Repla ce(".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" <Ly****@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
> Hi Darren,
> I should have posted my small datagrid with this so that you could see
> that
> I am using the name to link to the report in my datagrid. Here is my
> datagrid;
>
> <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?repor tID={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?
>
> "Darren Kopp" wrote:
>
>> Dim dirInfo as New DirectoryInfo(Server.MapPath("/Admin/report"))
>>
>> articleList.DataSource =
>> dirInfo.GetFiles("*.rpt").ToString().Replace(".rpt ", "");
>> articleList.DataBind()
>>
>> I think that works.
>>
>> -Darren
>>
>> "Lyners" <Ly****@discussions.microsoft.com> wrote in message
>> news:EA**********************************@microsof t.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
>>
>>
>>


Nov 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pluto | last post by:
Hi, I want to get a list of files matching certain extensions, such as "*.jpg;*.gif" (i.e. all files ending with .jpg and .gif). I was using DirectoryInfo.GetFiles(string) method....
1
by: Vagabond Software | last post by:
I am recursing through ALL folders and sub-folders below a certain level to list all the files of a certain type in those folders. I use two ArrayLists, alFiles and alFolders, to track matching...
1
by: siddharth_jain_1 | last post by:
Hello I am trying to get a list of shared files and subdirectories in a particular folder on a server. For this I am using DirectoryInfo in the following way. DirectoryInfo folder = new...
3
by: xenophon | last post by:
This following innocuous code: System.IO.DirectoryInfo fff = new System.IO.DirectoryInfo(); System.IO.FileInfo ppp = fff.GetFiles( Request.MapPath(".") ); for( int ccc=0 ; ccc < ppp.Length ;...
1
by: rn5a | last post by:
Assume that a custom server control derived from System.Web.UI.WebControls.WebControl encapsulates a Button & a TextBox. The Button & the TextBox controls are added using the CreateChildControls...
2
by: rn5a | last post by:
Suppose I have the following code: <script runat="server"> Sub Page_Load(.....) Dim dInfo As DirectoryInfo Dim fsi As FileSystemInfo dInfo = New DirectoryInfo(Server.MapPath("Folder1")) ...
0
by: MP | last post by:
Hi, I am getting the Access denied Error on a folder after I try to delete a folder that contains some files through my ASP.NET Application. Following is my code DirectoryInfo directoryInfo...
3
by: Steve Kershaw | last post by:
Hi, I need a way to bind a DirectoryInfo array to a GridView without any errors. The code I'm using to create the DirectoryInfo array is: // Define the current directory DirectoryInfo dir...
5
by: Tom P. | last post by:
I am having the following problem: I create a FileSystemWatcher and wait for events. When the event does happen I refresh a FileSystemInfo list and set properties accordingly (IsFile, IsDir,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.