Getting Server, Database and Browser Info in .NET

by Les Kendall. Many of my IT friends keep asking me to show a little information inside my apps so that they can see if it’s installed in the right place with the right params. This code is not quite as detailed as PHPInfo but it gives some relevant information to our IT support colleagues and gives you a start at least.  In my own code, I also add in some application specific information too (but not shown in the code below).


public string ServerInformation()
{
// Les Kendall. Free to use ‘as is’.
// you’ll need ‘using System’ and ‘using System.Reflection’ at top of page

StringBuilder sb = new StringBuilder();

sb.Append( “Note: Server Information is limited.” );

try
{
Assembly web = Assembly.GetExecutingAssembly();
AssemblyName webName = web.GetName();

sb.Append( “<br><br><b>SERVER INFO</b>” );
sb.Append( “<br>Server Name: ” + System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName() );
sb.Append( “<br>Running .NET Version: ” + System.Environment.Version );
sb.Append( “<br>Code Assembly Version: ” + webName.Version.ToString() );
sb.Append( “<br>Server Processors: ” + System.Environment.ProcessorCount.ToString() );
sb.Append( “<br>Server Current Directory: ” + System.Environment.CurrentDirectory );
sb.Append( “<br>Server User Domain Name: ” + System.Environment.UserDomainName );
sb.Append( “<br>Server Physical Path: ” + System.Web.Hosting.HostingEnvironment.ApplicationHost.GetPhysicalPath() );
}
catch
{
sb.Append( “<br>Error reading Server information” );
}

try
{
sb.Append( “<br><br><b>DATABASE INFO</b>” );

// CDBSqlUtils is my class that returns the SQLConnection
// from a connection string in the web.config
// as in: SqlConnection DBConnection = new SqlConnection( connectionString )
CDBSqlUtils db = new CDBSqlUtils();

if ( db == null )
{
sb.Append( “<br>Database driver is not available, which is strange!” );
}
else
{
// connection needs to be open to read properties.
db.DBConnection.Open();
sb.Append( “<br>Database: ” + db.DBConnection.Database );
sb.Append( “<br>Type: Microsoft SQL Server” );
sb.Append( “<br>SQL Server Version: ” + db.DBConnection.ServerVersion );
// WARNING: prints connection string and may show password
sb.Append( “<br>Connection String: ” + db.DBConnection.ConnectionString );
sb.Append( “<br>Connection Timeout: ” + db.DBConnection.ConnectionTimeout );
sb.Append( “<br>WorkstationId: ” + db.DBConnection.WorkstationId );
db.DBConnection.Close();

}
}
catch
{
sb.Append( “<br>Error reading Database information” );
}

try
{
HttpBrowserCapabilities bc;

bc = Request.Browser;
sb.Append( “<br><br><b>YOUR BROWSER</b>” );
sb.Append( “<br>Type = ” + bc.Type );
sb.Append( “<br>Name = ” + bc.Browser );
sb.Append( “<br>Version = ” + bc.Version );
sb.Append( “<br>Major Version = ” + bc.MajorVersion );
sb.Append( “<br>Minor Version = ” + bc.MinorVersion );
sb.Append( “<br>Platform = ” + bc.Platform );
sb.Append( “<br>Is Beta = ” + bc.Beta );
sb.Append( “<br>Is Crawler = ” + bc.Crawler );
sb.Append( “<br>Is AOL = ” + bc.AOL );
sb.Append( “<br>Is Win16 = ” + bc.Win16 );
sb.Append( “<br>Is Win32 = ” + bc.Win32 );
sb.Append( “<br>Supports Frames = ” + bc.Frames );
sb.Append( “<br>Supports Tables = ” + bc.Tables );
sb.Append( “<br>Supports Cookies = ” + bc.Cookies );
sb.Append( “<br>Supports VB Script = ” + bc.VBScript );
sb.Append( “<br>Supports JavaScript = ” + ( bc.MajorVersion >= 1 ? “true” : “false” ) );
sb.Append( “<br>Supports Java Applets = ” + bc.JavaApplets );
sb.Append( “<br>Supports ActiveX Controls = ” + bc.ActiveXControls );
}
catch
{
sb.Append( “<br>Error reading your Browser information” );
}

return sb.ToString();
}