Access Virtual Directory folder through code behind Asp.net

15,621

You can do it this way:

DirectoryInfo dir= new DirectoryInfo(Server.MapPath("/allimages"));

Then you can get the Files in this folder as below:

FileInfo[] files = dir.GetFiles(string searchPattern,SearchOption searchOption);

and for tha count of files you can simply do array count.

Share:
15,621
thebernardlim
Author by

thebernardlim

Updated on June 22, 2022

Comments

  • thebernardlim
    thebernardlim almost 2 years

    I am trying to access a Virtual Directory folder from Code-behind.

    • ASP.Net Website Name : SuperImages
    • Physical folder : C:\images
    • Virtual Directory folder : allimages (In same level as App_Data, Scripts, Properties folders)

    I am trying to access and do a count of the number of items in this folder, then display them on a webpage.

    How should I do this?

    Thanks in advance!

    =======================================================================

    Update : From the posts below, it seems that Server.MapPath would give me the correct physical path. However, it seems to me that I am getting the wrong physical path. Reason should be I am running 'debug' mode.

    Hence, any idea how I can ensure Server.MapPath point correctly and while running in debug mode?

    ======================================================================

    SOLUTION :

    Problem was that on Debug mode, I was using the VS Dev Server instead of my local IIS. I re-created a Virtual Directory for my app in the local IIS. Re-created another virtual directory for the 'allimages' folder in this newly created app, and it solved the problem.

  • thebernardlim
    thebernardlim about 11 years
    Hi, think this should work. However I realised that Server.MapPath keeps returning me the wrong physical path. Reason should be I am running 'debug' mode. Hence, any idea how I can ensure Server.MapPath point correctly and while running in debug mode? Thanks.
  • Bhushan Firake
    Bhushan Firake about 11 years
    @juniordeveloper87 You said that your allimages folder is at the same level of App_Data? So it should work , or your path may be different. Check it once again.