I am trying to access a Virtual Directory folder from Code-behind.
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.
In the Internet Information Services window, expand server name (where server name is the name of the server). Right-click the Web site that you want (for example, Default Web Site), point to New, and then click Virtual Directory.
A virtual directory is a directory name that you specify in IIS and map to physical directory on a local server's hard drive or a directory on another server (remote server). You can use Internet Information Services Manager to create a virtual directory for an ASP.NET Web application that is hosted in IIS.
In Visual Studio, on the File menu, click New and then click Web Site. In the New Web Site dialog box, click Browse. In the Choose Location dialog box, click the Local IIS icon. You must be a member of the Administrators group on the local computer to access the IIS metabase and create a virtual directory.
A virtual folder does not contain the actual document; it only holds a pointer to the document. All virtual folders are private (unique to the user) unless they are made public.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With