views:

422

answers:

3

Hi All

I have a web app where users can upload files. The files are physically stored by IIS in a virtual folder that is mapped to an external storage device. A record about each uploaded file is stored in the database. The database record contains information about whether the file is still "active" (hasn't been deleted by the user), and the virtual folder path (ex: /storage1/test)

Now, I would like to, periodically, run an administrative task outside IIS that retrieves a list of all files that are no longer "active" and deletes these from physical storage. I would like the administrative task to run outside IIS as a scheduled task or windows service. However, I cannot figure out how to map the virtual folder path that stored in the database record to a physical path, in the external process. Is there any way to "tap" into IIS from an external process or any other smart way to do this? (or am I going in the wrong direction altogether).

TIA /Henrik

A: 

If your app is an ASP.NET app, you could investigate the Server.MapPath call - if you're using the same virtual directory as the main app.

Otherwise, I'd suggest storing the "base path" (that corresponds to the virtual directory path) in a config for your external app and just concatenating that base path and the file path into a complete path.

Marc

marc_s
A: 

If you need to retrieve this path programmatically then you can do something like:

using(DirectoryEntry de = 
    new DirectoryEntry("IIS://Localhost/w3svc/1/root/storage1/test"))
{
    string pathToFiles = de.Properties["Path"].Value;

    // Do my file tidy up tasks....
}

There are a couple of things to note:

  • The number '1' in the path of the DirectoryEntry constructor is the IIS number of the site.

  • In the path IIS://Localhost/w3svc/1/root/storage1/test, the first part IIS://Localhost/w3svc/1/root is your website's 'root' application. You always need this part.

  • You would need to add a reference to the System.DirectoryServices assembly to your project.

HTH
Kev

Kev
A: 

You'll need to add a reference to System.Web in your project.

string path = System.Web.HttpServerUtility.MapPath("/MyWebSite");
Some Canuck
How'd an accepted correct answer be reduced to 0?
Some Canuck