Click here to Skip to main content
15,907,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am listing all the files from directory in a web page.
I have done it successfully.
Now I want to open the files from webpage but I am unable to do this.
My code is below:

C#
DirectoryInfo directory = new DirectoryInfo("C:/files/");
                FileSystemInfo[] files = directory.GetFiles("*.txt");
                var OrderedFiles = files.OrderByDescending(f => f.Name);
                foreach (FileSystemInfo file in OrderedFiles)
                {
                    var link = new HyperLink { };
                    link.NavigateUrl = "C:/files/" + file.Name;
                    link.Text =    file.Name    ;

                    Panel1.Controls.Add(link);



Thanks in advance
Posted

Weird: just had to knock something like this up yesterday:

Use an HtmlAnchor control as opposed to a HyperLink control:

link = new HtmlAnchor();
link.HRef = ResolveUrl(path + "\\" + file.Name);
link.Target = "_blank";
link.InnerText = file.Name;


where path might be like:

@"~\Reports\Documents";
 
Share this answer
 
Comments
neha427 13-Jul-11 10:34am    
Hi,
I want to open file as a text file when i click on the web page.
Please, can you suggest me how to do this.
R. Giskard Reventlov 13-Jul-11 11:04am    
Just click on it!
Digital man said it.

But it might just be a small error. Like you should put the @ sign before your strings that have / sign or \\ sign
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900