简体   繁体   中英

C# - is there a way to get the local path of the Default FTP site?

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?

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();
        }

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

替代文字

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM