Blog

30May
2007
GetDriveInfo() UDF Powered By .NET

This is an example that I used when demonstrating ColdFusion 8 .NET integration on the recent usergroup tour, and as requested, I am posting it publicly. GetDriveInfo() returns a query containing specifics about the hard drives on your server, it returns all drives unless an optional drive letter is passed to it. GetDriveInfo() uses the .NET System.IO.DriveInfo class (which was introduced in .NET 2, and thus this example requires .NET 2 or 3).

Here is the code:

view plain print about
1<!--- Get drive details for one or all drives --->
2<cffunction name="GetDriveInfo" returntype="query" output="false">
3    <cfargument name="drive" required="no" default="">
4
5    <!--- Local vars --->
6    <cfset var result=QueryNew("name,type,isready,format,label,totalsize,freespace",
7                            "varchar,varchar,bit,varchar,varchar,double,double")
>

8    <cfset var sidiClass="">
9    <cfset var drives="">
10    <cfset var i=0>
11
12    <!--- Get System.IO.DriveInfo class --->
13    <cfobject type=".NET"
14                name="sidiClass"
15                class="System.IO.DriveInfo">

16    <!--- Get drives --->
17    <cfset drives=sidiClass.GetDrives()>
18
19    <!--- Loop through drives --->
20    <cfloop from="1" to="#ArrayLen(drives)#" index="i">
21        <!--- Check if need this one --->
22        <cfif ARGUMENTS.drive IS ""
23            OR ARGUMENTS.drive EQ drives[i].Get_Name()
24            OR (Len(ARGUMENTS.drive) IS 1
25                AND ARGUMENTS.drive EQ Left(drives[i].Get_Name(), 1))>

26            <!--- Add row --->
27            <cfset QueryAddRow(result)>
28            <!--- Get name, type, and ready flag --->
29            <cfset QuerySetCell(result, "name", drives[i].Get_Name())>
30            <cfset QuerySetCell(result, "type", drives[i].Get_DriveType().ToString())>
31            <cfset QuerySetCell(result, "isready", drives[i].Get_IsReady())>
32            <!--- Get extra details ONLY if ready, or will throw error --->
33            <cfif drives[i].Get_IsReady()>
34                <cfset QuerySetCell(result, "format", drives[i].Get_DriveFormat())>
35                <cfset QuerySetCell(result, "label", drives[i].Get_VolumeLabel())>
36                <cfset QuerySetCell(result, "totalsize", drives[i].Get_TotalSize())>
37                <cfset QuerySetCell(result, "freespace", drives[i].Get_AvailableFreeSpace())>
38            </cfif>
39        </cfif>
40    </cfloop>
41
42    <!--- Return result --->
43    <cfreturn result>
44
45</cffunction>

And here is a simple test example:

view plain print about
1<!--- Test with all drives --->
2<h3>All Drives</h3>
3<cfdump var="#GetDriveInfo()#">
4
5<!--- Test with just C: drive --->
6<h3>C: Drive</h3>
7<cfdump var="#GetDriveInfo("C")#">
8
9<!--- Display just space on C: drive --->
10<h3>Free Space On C Drive</h3>
11<cfdump var="#NumberFormat(GetDriveInfo("C").freespace)#">

Related Blog Entries

Comments (7)



  • Ryan Guill

    So I get that we can call .net code from cf, but can it work the other way around? Can someone in .net call some code that I write in CF through the same pipes?

  • Damon Cooper

    That'd be the .NET Gateway we didn't have time for :)

    CF9?

    Dmaon

  • Ryan Guill

    Ah, thanks Damon, that answers it.

  • Ben Forta

    Ryan, which means that for now you'll need to use Web Services.

    --- Ben

    #4Posted by Ben Forta | May 30, 2007, 03:15 PM
  • Rupesh Kumar

    Why not let Microsoft build the capability to invoke CF component from .NET ;-)

    On a serious note, I think the only way to do that currently is through webservices.

  • Ryan Guill

    Ha, I can see microsoft jumping at that chance... I can see the marketing now....

    If you invest in .net, you can do your heavy lifting in coldfusion!

    Seriously, thanks for your work on all of this guys, I can live with webservices for a little while longer, but here's hoping for CF 9! ;)

  • Jeremy

    Anyone know if there is a TCP/IP .Net assembly? I'm look to send a tcp/ip message to a server and receive a response from the server.

    Thanks

    #7Posted by Jeremy | Oct 15, 2007, 09:50 AM