views:

235

answers:

3

Hi,

Is there a way to get the local path of the Default FTP site (in IIS) programmatically?

Like C:\program files\ftproot, shown below:

alt text

I'd imagine it would be something like:

DirectoryEntry ftproot = new DirectoryEntry("IIS://localhost/MSFTPSVC/1/Root");
string directory; // = ftproot.something

Any ideas?

Edit: This would be for IIS 6.0. Surely this has got to be stored somewhere - maybe in the registry?

A: 

This article might help (IIS7 only):

Code in the second reply.

GrayWizardx
A: 

For IIS 6 at least, I found it in the registry here:

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\MSFtpsvc\Parameters\Virtual Roots\

Format of the data is a little strange - for instance, D:\ftproot,,1

alt text

David Hodgson
I'm curious as to what that middle value is in D:\ftproot,,1. It looks like that last value is the sum of the read and write rights, where read = 1, and write = 2.
David Hodgson
+1  A: 

From what I know, there are two Active Directory attributes: msIIS-FTPRoot, msIIS-FTPDir.

From Technet

Basically, the user's home folder is determined upon authentication by querying the msIIS-FTPRoot and msIIS-FTPDir attributes of the user object in Active Directory. The concatenation of the msIIS-FTPRoot and msIIS-FTPDir values results in the path to the user's home folder.

An example may look like this:

  msIIS-FTPRoot = D:\FTP Users
  msIIS-FTPDir = \JohnSmith

This will result in "D:\FTP Users\JohnSmith" as the home folder for the user.

Code to traverse all the users and there default directories:

    static void Main(string[] args)
            {            
                string domain = Environment.GetEnvironmentVariable("USERDNSDOMAIN");
                string dc = GetDC(domain);
                string ldap = String.Format("LDAP://{0}/{1}", domain, dc);
                DirectoryEntry e = new DirectoryEntry(ldap);

                DirectorySearcher src = new DirectorySearcher(e, "(objectClass=user)");
                SearchResultCollection res = src.FindAll();
                foreach (SearchResult r in res)
                {
                    DirectoryEntry f = r.GetDirectoryEntry();
                    Console.WriteLine(f.Name + "\t" + f.Properties["msIIS-FTPRoot"].Value + f.Properties["msIIS-FTPDir"].Value);
                }
                Console.ReadKey();
            }

private static string GetDC(string domain)
        {
            StringBuilder sb = new StringBuilder(domain);
            sb.Replace(".", ",DC=");
            sb.Insert(0, "DC=");
            return sb.ToString();
        }
Aseem Gautam
Sorry, where is GetDC() defined?
David Hodgson
Sorry.. Code is updated.
Aseem Gautam