Identify IIS Sites and Log File locations for WWW and FTP -the source
Exactly a year ago today I posted a little application that output the sites in IIS to a text file and as a few days ago Lars asked for the source, I thought it would be a nice thing to release it exactly a year later.
I didn't plan it that way, it just happened! Cool :)
Identify IIS Sites and Log File locations for WWW and FTP sourceusing System; using System.DirectoryServices; using System.IO; using System.Collections; namespace IISSites {...}{ class Program {...} { staticstring fileToWrite =String.Empty; [STAThread] staticvoid Main(string[] args) {...} { fileToWrite =String.Format("IISExport{0:dd-MM-yyyy}.txt", DateTime.Today); if (args !=null && args.Length >0) {...} { fileToWrite = args[0]; } SortedList www =new SortedList(); SortedList ftp =new SortedList(); try{...} { conststring FtpServerSchema ="IIsFtpServer"; // Case Sensitiveconststring WebServerSchema ="IIsWebServer"; // Case Sensitivestring ServerName ="LocalHost"; DirectoryEntry W3SVC =new DirectoryEntry("IIS://"+ ServerName +"/w3svc", "Domain/UserCode", "Password"); foreach (DirectoryEntry Site in W3SVC.Children) {...} { if (Site.SchemaClassName == WebServerSchema) {...} { string LogFilePath = System.IO.Path.Combine( Site.Properties["LogFileDirectory"].Value.ToString(), "W3SVC"+ Site.Name); www.Add(Site.Properties["ServerComment"].Value.ToString(), LogFilePath); } } DirectoryEntry MSFTPSVC =new DirectoryEntry("IIS://"+ ServerName +"/msftpsvc"); foreach (DirectoryEntry Site in MSFTPSVC.Children) {...} { if (Site.SchemaClassName == FtpServerSchema) {...} { string LogFilePath = System.IO.Path.Combine( Site.Properties["LogFileDirectory"].Value.ToString(), "MSFTPSVC"+ Site.Name); ftp.Add(Site.Properties["ServerComment"].Value.ToString(), LogFilePath); } }int MaxWidth =0; foreach (string Site in www.Keys) {...} { if (Site.Length > MaxWidth) MaxWidth = Site.Length; }foreach (string Site in ftp.Keys) {...} { if (Site.Length > MaxWidth) MaxWidth = Site.Length; } OutputIt("SiteDescription".PadRight(MaxWidth) +" Log File Directory"); OutputIt("".PadRight(79, '=')); OutputIt(String.Empty); OutputIt("WWWSites"); OutputIt("========="); foreach (string Site in www.Keys) {...} { string output = Site.PadRight(MaxWidth) +" "+ www[Site]; Console.WriteLine(output); OutputIt(output); }if (ftp.Keys.Count >0) {...} { OutputIt(String.Empty); OutputIt("FTPSites"); OutputIt("========="); foreach (string Site in ftp.Keys) {...} { string output = Site.PadRight(MaxWidth) +" "+ ftp[Site]; OutputIt(output); } } }// Catch any errorscatch (Exception e) {...} { Console.WriteLine("Error:"+ e.ToString()); }finally{...} { Console.WriteLine(); Console.WriteLine("Pressenter to close/exit..."); //Console.Read(); } }staticvoid OutputIt(string lineToAdd) {...} { Console.WriteLine(lineToAdd); if (!String.IsNullOrEmpty(fileToWrite)) {...} { StreamWriter SW; SW = File.AppendText(fileToWrite); SW.WriteLine(lineToAdd); SW.Close(); }else{...} { Console.WriteLine("locationToOutputis Null or String.Empty please supply a value and try again."); } } }}