Getting System Info with Coldfusion 8 and .NET

On January 30, 2008, in ColdFusion, by Anuj Gakhar

This is yet another example of how native .NET objects can be easily consumed from Coldfusion 8. Recently, I have been spending some of my time on this particular aspect of Coldfusion 8, as is obvious from some of my posts. For those who are not aware, Coldfusion 8 supports calling .NET objects simply by using the CFOBJECT tag or the CreateObject() method. You need to pass in the .NET assembly (ideally a .dll file) and the class of that assembly, as parameters to the CFOBJECT tag. By default, mscorlib (which is a .NET core assembly) is always included so there is no need to specify that if the class you are using happens to be one of the classes from this assembly.

In this example, I am going to demonstrate how to get some basic information about your machine using the System.Environment class. We start by creating the object.

[xml]<CFTRY>
<CFOBJECT TYPE=".NET" CLASS="System.Environment" NAME="ENV" ACTION="CONNECT">
<CFCATCH type="ANY">
<CFOBJECT TYPE=".NET" CLASS="System.Environment" NAME="ENV" ACTION="CREATE">
</CFCATCH>
</CFTRY>[/xml]

Now, lets try and get some information out of this.

[xml]<cfset stargs = StructNew()>
<cfset stargs.cwd = env.Get_CurrentDirectory()>
<cfset stargs.machineName = env.Get_MachineName()>
<cfset stargs.platform = env.Get_OsVersion().Get_Platform().ToString()>
<cfset stargs.servicepack = env.Get_OsVersion().Get_ServicePack()>
<cfset stargs.versionString = env.Get_OsVersion().Get_VersionString()>
<cfset stargs.processorCount = env.Get_ProcessorCount()>
<cfset stargs.systemDirectory = env.Get_SystemDirectory()>
<cfset stargs.newline = env.Get_NewLine()>
<cfset stargs.logicalDrives = env.GetLogicalDrives()>
<cfset stargs.envVariables = env.GetEnvironmentVariables()>
<cfdump var = "#stargs#">[/xml]

As you would guess, its all about knowing the right function names in the right assembly. Once that bit is done, the coding is pretty straightforward. There is some information available in this System.Environment assembly but I have only used the ones I thought could be useful.

Any thoughts?

Tagged with:  

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar
%d bloggers like this: