views:

245

answers:

3
protected void Button1_Click(object sender, EventArgs e)
{
    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"C:\Documents and Settings\Admin\Desktop\New Folder\"+TextBox1.Text);
    foreach (System.IO.FileInfo file in dir.GetFiles())
    {
        HyperLink h = new HyperLink();
        h.NavigateUrl = "file:///c:/Documents and Settings/Admin/Desktop/New Folder/" + TextBox1.Text + "/" + file.Name;


        h.Text = file.Name;
        PlaceHolder1.Controls.Add(h);
    }


}

On execution of this code hyperlinks get generated but they are not working. nothing happens when i click on them.

Please help.

+2  A: 

In ASP.NET hyperlinks must be URLs not a folder on the computer.

If your file is in your site try Sever.MapPath

David Basarab
No my file is not in site.It may be anywhere in the system.
Akshay
@Akshay: Your code executes server-side and you're trying to have it work client-side? You should use something else; ASP.NET is not the tool for what you're apparently trying to do.
Randolpho
@Randolpho:But when we give url of any file which in website's folder then it works.The problem arises when hyperlink's navigateurl targets any external file in system which is not in websites folder.
Akshay
@Akshay: Exactly! The file has to be there if you want IIS to serve up the file. If you just provide a local file path to the client, they're going to try to look up a file *on their computer* not a file on the server.
Randolpho
A: 

This is due to security restrictions in the browser. If you generate a "file://" link, it is relative to the user's file system.

Theoretically, if browsers allowed these types of links, attackers could discover information about a user's file system remotely. Thus, modern browsers do not allow this type of link.

Unfortunately this is not very well documented, and most browsers allow the links and just drop the behaviour - so nothing happens when you click them. There aren't any good workarounds either.

See my question here for further discussion.

womp
A: 

Check out this question. The solution may work for you too.

J.Hendrix