Click here to Skip to main content
15,937,127 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
In my asp .net project I am trying to view my uploaded files stored in upload folder/directory it shows it but when I try to view the files stored in folder/sub directory of upload.
protected void Page_Load(object sender, EventArgs e)
        DirectoryInfo di = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("")+ "\\UserImages\\");
        int i = 0;
        FileInfo[] sortList = di.GetFiles();
        Array.Sort(sortList, new MyDateSorter());
        foreach (FileInfo fi in sortList)
            HyperLink HL = new HyperLink();

            Label lbl = new Label();
            Label lbl1 = new Label();
            HL.ID = "HyperLink" + i++;
            HL.Text ="View";
            lbl1.Text = fi.Name;
            lbl.Text = fi.CreationTime.ToShortDateString();
            //This line is not woking
          HL.NavigateUrl = Server.MapPath("") + "\\UserImages\\" + fi.Name;
            //This line is woking
           //HL.NavigateUrl =fi.Name;
            myPlaceHolder.Controls.Add(new LiteralControl("<span style='padding-left:20px'/>"));
            myPlaceHolder.Controls.Add(new LiteralControl("<br/>"));

[EDIT] Subject line decreased[/EDIT}
Updated 9-Mar-11 19:36pm
Sergey Alexandrovich Kryukov 10-Mar-11 1:31am    
m@dhu 10-Mar-11 1:37am    
What problem are you facing?

1 solution

//This line is not woking
          HL.NavigateUrl = Server.MapPath("") + "\\UserImages\\" + fi.Name;

Looks incorrect!

If UserImages is a folder, part of your application (inside your root node) then use (~) for root node and map the folder. Further, you can use this tip to correctly find the root node path and use it: Resolving Paths in a Multi-Folder WebSite[^]

All you need is to form the url correctly. Use the VS debugger as and when needed to look the URL formed and rectify it if needed.
Share this answer
vineesha 10-Mar-11 3:50am    
Now it is working
Sandeep Mewara 10-Mar-11 4:20am    
Good to know. :)

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