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 source
using System;using System.DirectoryServices;
using System.IO;
using System.Collections;
namespace IISSites
{
class Program
class Program
{
static string fileToWrite = String.Empty;
[STAThread]
static void Main(string[] args)
static void OutputIt(string lineToAdd)
}static string fileToWrite = String.Empty;
[STAThread]
static void Main(string[] args)
{
fileToWrite = String.Format("IISExport{0:dd-MM-yyyy}.txt", DateTime.Today);
if (args != null && args.Length > 0)
SortedList www = new SortedList();
SortedList ftp = new SortedList();
try
catch (Exception e)
fileToWrite = String.Format("IISExport{0:dd-MM-yyyy}.txt", DateTime.Today);
if (args != null && args.Length > 0)
{
fileToWrite = args[0];
}
fileToWrite = args[0];
}
SortedList www = new SortedList();
SortedList ftp = new SortedList();
try
{
const string FtpServerSchema = "IIsFtpServer"; // Case Sensitive
const string WebServerSchema = "IIsWebServer"; // Case Sensitive
string ServerName = "LocalHost";
DirectoryEntry W3SVC = new DirectoryEntry("IIS://" + ServerName + "/w3svc", "Domain/UserCode", "Password");
foreach (DirectoryEntry Site in W3SVC.Children)
DirectoryEntry MSFTPSVC = new DirectoryEntry("IIS://" + ServerName + "/msftpsvc");
foreach (DirectoryEntry Site in MSFTPSVC.Children)
foreach (string Site in www.Keys)
OutputIt("".PadRight(79, '='));
OutputIt(String.Empty);
OutputIt("WWW Sites");
OutputIt("=========");
foreach (string Site in www.Keys)
// Catch any errors const string FtpServerSchema = "IIsFtpServer"; // Case Sensitive
const string WebServerSchema = "IIsWebServer"; // Case Sensitive
string ServerName = "LocalHost";
DirectoryEntry W3SVC = new DirectoryEntry("IIS://" + ServerName + "/w3svc", "Domain/UserCode", "Password");
foreach (DirectoryEntry Site in W3SVC.Children)
{
if (Site.SchemaClassName == WebServerSchema)
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);
}
}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)
int MaxWidth = 0; 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);
}
}string LogFilePath = System.IO.Path.Combine(
Site.Properties["LogFileDirectory"].Value.ToString(),
"MSFTPSVC" + Site.Name);
ftp.Add(Site.Properties["ServerComment"].Value.ToString(), LogFilePath);
}
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;
}
{
if (Site.Length > MaxWidth)
MaxWidth = Site.Length;
}
OutputIt("Site Description".PadRight(MaxWidth) + " Log File Directory"); if (Site.Length > MaxWidth)
MaxWidth = Site.Length;
}
OutputIt("".PadRight(79, '='));
OutputIt(String.Empty);
OutputIt("WWW Sites");
OutputIt("=========");
foreach (string Site in www.Keys)
{
string output = Site.PadRight(MaxWidth) + " " + www[Site];
Console.WriteLine(output);
OutputIt(output);
}
if (ftp.Keys.Count > 0) string output = Site.PadRight(MaxWidth) + " " + www[Site];
Console.WriteLine(output);
OutputIt(output);
}
{
OutputIt(String.Empty);
OutputIt("FTP Sites");
OutputIt("=========");
foreach (string Site in ftp.Keys)
}OutputIt(String.Empty);
OutputIt("FTP Sites");
OutputIt("=========");
foreach (string Site in ftp.Keys)
{
string output = Site.PadRight(MaxWidth) + " " + ftp[Site];
OutputIt(output);
}
}string output = Site.PadRight(MaxWidth) + " " + ftp[Site];
OutputIt(output);
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.ToString());
}
finally Console.WriteLine("Error: " + e.ToString());
}
{
Console.WriteLine();
Console.WriteLine("Press enter to close/exit...");
//Console.Read();
}
}Console.WriteLine();
Console.WriteLine("Press enter to close/exit...");
//Console.Read();
}
static void OutputIt(string lineToAdd)
{
Console.WriteLine(lineToAdd);
if (!String.IsNullOrEmpty(fileToWrite))
}Console.WriteLine(lineToAdd);
if (!String.IsNullOrEmpty(fileToWrite))
{
StreamWriter SW;
SW = File.AppendText(fileToWrite);
SW.WriteLine(lineToAdd);
SW.Close();
}
else StreamWriter SW;
SW = File.AppendText(fileToWrite);
SW.WriteLine(lineToAdd);
SW.Close();
}
{
Console.WriteLine("locationToOutput is Null or String.Empty please supply a value and try again.");
}
}Console.WriteLine("locationToOutput is Null or String.Empty please supply a value and try again.");
}
Liked this post? Got a suggestion? Leave a comment