lakhi
asked on
Exclude Extensions from file array
I have an .aspx page that links to all the .pdf files in a directory and does not display the extension.
Now they would like to have all the files linked and still not display the file extension.
I'm thinking there would be an easier way to do this using "GetFileNameWithoutExtensi on" but I'm not exactly sure how to do it.
Thanks for any help
Now they would like to have all the files linked and still not display the file extension.
I'm thinking there would be an easier way to do this using "GetFileNameWithoutExtensi
Thanks for any help
Dim dirInfo as New DirectoryInfo(Server.MapPath("/p1/q/csr rpt"))
Dim arrFileInfo As Array
Dim filesInfo As FileInfo
Dim filesTable As New DataTable
Dim drFiles As DataRow
Dim dvFiles As DataView
Dim ofn,ext As String
Dim charCt as Integer
filesTable.Columns.Add("Name", Type.GetType("System.String"))
filesTable.Columns.Add("LastWriteTime", Type.GetType("System.DateTime"))
' Get File Info
arrFileInfo = dirInfo.GetFiles("*.pdf")
For Each filesInfo In arrFileInfo
drFiles = filesTable.NewRow()
ofn = filesInfo.Name
ext = Right(ofn, 4)
charCt = Instr(ofn, ".") -1
ofn = Mid(ofn, 1, charCt)
drFiles("Name") = ofn
drFiles("LastWriteTime") = filesInfo.LastWriteTime
filesTable.Rows.Add(drFiles)
Next filesInfo
dvFiles = filesTable.DefaultView
dvFiles.Sort = "LastWriteTime DESC"
dg1.DataSource = dvFiles
dg1.DataBind()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wait a sec - that might have been something I did. BRB
Look closely at the line.. =)
drFiles("Name") = System.IO.Path.GetFileName WithoutExt ension(fil esInfo.Nam e)
drFiles("Name") = System.IO.Path.GetFileName
ASKER
I did get it to work.
Now, the issue is to add the extension back in when I set up the "DataNavigateUrlFormatStri ng" in the datagrid where the links are listed.
When they were all pdf files, I could just use {0}.pdf but now I'm not sure. How can I do that?
The issue is a little trickier for me because the links are in a virtual directory on the intranet server. So I do a server.mappath to the virtual directory but then the "DataNavigateUrlFormatStri ng" will be:
"http://myintranet.com/virtualDir/qty/csr/2008/" and I used to just add the "/{0}.pdf " but of course that doesn't work now.
help?
Now, the issue is to add the extension back in when I set up the "DataNavigateUrlFormatStri
When they were all pdf files, I could just use {0}.pdf but now I'm not sure. How can I do that?
The issue is a little trickier for me because the links are in a virtual directory on the intranet server. So I do a server.mappath to the virtual directory but then the "DataNavigateUrlFormatStri
"http://myintranet.com/virtualDir/qty/csr/2008/" and I used to just add the "/{0}.pdf " but of course that doesn't work now.
help?
Add the actual FileInfo instance to your Table:
filesTable.Columns.Add("Fi leInfo", Type.GetType("System.IO.Fi leInfo"))
Then in the loop:
drFiles("FileInfo") = filesInfo
Now you can get it back out with a cast:
Dim fi As FileInfo = CType(YourDataRowHere("Fil eInfo"), FileInfo)
Dim FullName As String = fi.FullName
' ...do something with "FullName"...
filesTable.Columns.Add("Fi
Then in the loop:
drFiles("FileInfo") = filesInfo
Now you can get it back out with a cast:
Dim fi As FileInfo = CType(YourDataRowHere("Fil
Dim FullName As String = fi.FullName
' ...do something with "FullName"...
ASKER
Sorry - I'm still a bit confused. I understand how to get the filename with and without the extension but when I set up the datagrid link, I get lost.
If I have:
Dim fi As FileInfo = CType(YourDataRowHere("Fil eInfo"), FileInfo)
Dim FullName As String = fi.FullName
Then how do I reference it in the datagrid hyperlink column. Here is what I mean:
<asp:HyperLinkColumn DataNavigateUrlField="Name " DataNavigateUrlFormatStrin g="http://myintranet.com/virtualDir/qty/csr/2008/" **** DataTextField="Name" Target="_blank">
**** Can I just put "FullName" at this point or how do I do that?
Sorry to be a pest...
If I have:
Dim fi As FileInfo = CType(YourDataRowHere("Fil
Dim FullName As String = fi.FullName
Then how do I reference it in the datagrid hyperlink column. Here is what I mean:
<asp:HyperLinkColumn DataNavigateUrlField="Name
**** Can I just put "FullName" at this point or how do I do that?
Sorry to be a pest...
(you're not being a pest)
*** I'm NOT a web programmer though *** =(
Couldn't you just add an extra column to your datatable that has the filename with the extension on it?
filesTable.Columns.Add("Fi leName", Type.GetType("System.Strin g"))
...
drFiles("FileName") = filesInfo.Name ' This WILL have the extension on it
Then you could use your previous code that worked for just PDFs, but with "FileName" instead of "Name".
Sorry if that makes no sense. Like I said, I'm not a web programmer. =\
*** I'm NOT a web programmer though *** =(
Couldn't you just add an extra column to your datatable that has the filename with the extension on it?
filesTable.Columns.Add("Fi
...
drFiles("FileName") = filesInfo.Name ' This WILL have the extension on it
Then you could use your previous code that worked for just PDFs, but with "FileName" instead of "Name".
Sorry if that makes no sense. Like I said, I'm not a web programmer. =\
ASKER
I did post my question in the "Programming for ASP.Net"
If you aren't a web programmer, who else do I ask?
If you aren't a web programmer, who else do I ask?
You also posted in the GENERIC .Net Topic areas with a code snippet dealing almost exclusively with non web specific code. I looked at your question and it seemed like I could answer just the part you needed w/o knowing any web programming...and I did. I showed how to use System.IO.GetFileNameWitho utExtensio n().
I'll hit the "Request Attention" button and they can get more attention to your question.
Apologies...
I'll hit the "Request Attention" button and they can get more attention to your question.
Apologies...
ASKER
Oh! That makes sense.
Obviously, I'm new at this.
I think I will post it as a separate question because you were so helpful with my main question.
Thank you very much.
Obviously, I'm new at this.
I think I will post it as a separate question because you were so helpful with my main question.
Thank you very much.
ASKER
Very helpful. I really needed a web programmer but I didn't post my question in the right place and the Expert who helped me did a great job! Thanks!
ASKER
BC30456: 'GetFileNameWithoutExtensi