• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 833
  • Last Modified:

How To add Hyperlink to all files in a folder

I have a folder that contains several PDF files.
I want to be able to add a hyperlink to each file on a page.
Files will be added to the folder so I want to be able to add the links at runtime.
Does anyone have any VB 2005 examples of this.
Thanks
0
whiwex
Asked:
whiwex
2 Solutions
 
whiwexAuthor Commented:
I figured out how to read each file name. But how do I add the Hyperlink to the page?
0
 
amit_gCommented:
The link I posted would make a link and a download button. Basically you need to use some datacontrol e.g. DataGrid, Repeater, DataList. The link uses DataGrid. you can use anything you like.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
whiwexAuthor Commented:
I'm trying to do this with Vb 2005
0
 
amit_gCommented:
As a desktop application?
0
 
whiwexAuthor Commented:
Here's thecode I'm using. It adds a Link but it will not navigate to the file.
Partial Class Tidbits
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim link As New HyperLink
        Dim file As String
        Dim files() As String = System.IO.Directory.GetFiles("C:\Inetpub\wwwstestsite\Tidbits\", "*.pdf", IO.SearchOption.TopDirectoryOnly) 'Gets all Files that are txt
        Dim top As Integer
        Dim left As Integer
        top = 250
        Left = 100
        For Each file In files
            link = New HyperLink
            link.NavigateUrl = file
            link.Text = file
            link.Style("Position") = "absolute"
            link.Style("Left") = (left & "px")
            link.Style("Top") = (top & "px")
            link.Font.Bold = True
            link.Font.Size = 12
            link.Font.Italic = False
            link.Font.Name = "Times New Roman"
            link.Font.Overline = False
            link.Font.Strikeout = False
            link.Font.Underline = False
            Me.Controls.Add(link)
            top = top + 50
        Next
     End Sub
0
 
amit_gCommented:
Change

link.NavigateUrl = file

to

link.NavigateUrl = "file://" & file
0
 
whiwexAuthor Commented:
The file doesn't open
0
 
RobSampsonCommented:
Try changing the lines below:
link.NavigateUrl = file
link.Text = file

to
link.NavigateUrl = file.Path
link.Text = file.Path

or whatever the VB equivalent is of a file object.  In VBScript, when you speicify Path you get the entire full path to the file.  When you are just using "file", you are invoking the default property of a file object (if any), which is likely not the Path property.

Regards,

Rob.
0
 
amit_gCommented:
Change it back to

link.NavigateUrl = file.Name

and change

link.Text = file

to

link.Text = file.Name

Now when you see those links, take the cursor to the link. What URL do you see in the statusbar? If you don't see any atatusbar, click the link. What URL do you see the address bar? The pdf files are in C:\Inetpub\wwwstestsite\Tidbits\. If your page is also in the same folder then this should have worked. If your page is in C:\Inetpub\wwwstestsite\ folder then you need to change

link.NavigateUrl = file.Name

to

link.NavigateUrl = "Tidbits/" & file.Name

Basically the URL is relative to the location where your aspx page is and that is why you may have to sub-foldername
0
 
whiwexAuthor Commented:
File is a string that contains full path name of the file. So I can't do file.name.

While passover the link on the page the path is file:///C:\Inetpub\wwwstestsite\Tidbits\.test.pdf and nothing happens when I click the link.
If I change link.NavigateUrl = "Http:// & file then the path is http:///C:\Inetpub\wwwstestsite\Tidbits\.test.pd 
the path should be http://localhost:1285/file:///C:\Inetpub\wwwstestsite\Tidbits\.test.pd
0
 
amit_gCommented:
Oh I should have seen that already. You need to use DirectoryInfo class instead of Directory class.

        Dim file As System.IO.FileInfo
        Dim files() As FileInfo = System.IO.DirectoryInfo.GetFiles("C:\Inetpub\wwwstestsite\Tidbits\", "*.pdf", IO.SearchOption.TopDirectoryOnly) 'Gets all Files that are txt

and then use file.name as I suggested.
0
 
whiwexAuthor Commented:
Dim files() As FileInfo is not an option
I added imports system.IO.Filefo still no fileinfo
0
 
amit_gCommented:
Use System.IO.FileInfo
0
 
deaditeCommented:
Here's the code you need plus it has a few extra options.  I used to use this on an ASP website to autogenerate my hyperlinks:

<%

 ' ****************************************************************
 ' Code to Auto Hyperlink Files In The Specified Directory
 ' ****************************************************************

      ' You may first create the FileSystemObject.
      Dim Filefso
      Set Filefso = Server.CreateObject("Scripting.FileSystemObject")


      ' Set the reference of the current folder
      ' using FileSystemObject's GetFolder method.
      ' Examples: "/" is wwwroot "/subfolder is a subfolder in wwwroot
        '           "./" is current folder and "./subfolder" is a subfolder in current folder

      Dim FileFolder,FileList
      set FileFolder = Filefso.GetFolder(Server.MapPath("./Files"))
      set FileList = FileFolder.Files

      ' This Part hyperlinks the Files
      Response.Write("<ul>")
      ' Auto Hyperlink To the Current Folder
        Response.Write("<li><a href='./'>Home</a></li>")
      For Each FileIndex in FileList
        'This Explicitly Defines and Excludes Hyperlinking Certain File Extensions and File
      ' This Does Not Link Files Which have a "_" As the First Character
        hid = (InStrRev(FileIndex.Name,"_"))
        If hid = 1 then
        ' Do Nothing
      'Add An Elseif for Each File Extension or File to not hyperlink
        Elseif Lcase(right(FileIndex.Name, 4)) = ".txt" then
      ' Do Nothing
        Elseif Lcase(right(FileIndex.Name, 4)) = ".ico" then
      ' Do Nothing
        Elseif Lcase(right(FileIndex.Name, 4)) = ".jpg" then
      ' Do Nothing
        Elseif Lcase(right(FileIndex.Name, 5)) = ".jpeg" then
      ' Do Nothing
        Elseif Lcase(right(FileIndex.Name, 4)) = ".gif" then
      ' Do Nothing
        Elseif Lcase(right(FileIndex.Name, 4)) = ".png" then
      ' Do Nothing
        Elseif FileIndex.Name = "sublinks.asp" then
      ' Do Nothing
        Elseif FileIndex.Name = "default.asp" then
      ' Do Nothing
      'This Hyperlinks All Other Files
        Else
          Response.Write "<li><a href ='" & FileIndex.Name & "'>" & FileIndex.Name & "</a></li>"
        End if
      Next
      Response.Write("</ul>")


      ' Sets Objects to Nothing
      Set Filefso = nothing
      Set FileFolder = nothing
      Set FileList = nothing
 %>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now