using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO; /// to bring in the DriveInfo class
using System.Collections; /// bring in the IEnumberable defintion
 
public partial class UserDefinedFunctions
{
    /// <summary>
    /// returns informatoin on alal drives in the system
    /// </summary>
    /// <returns></returns>
    [SqlFunction(FillRowMethodName = "FillRow",
        TableDefinition ="letter nchar(1),total_mb bigint null," +
                         "free_mb bigint null,format nvarchar(32) null," +
                         "type nvarchar(32) null," +
                         "volume_label nvarchar(20) null")]
    public static IEnumerable drive_info()
    {
 
        return System.IO.DriveInfo.GetDrives();
    }
 
    public static void FillRow(Object obj
                             , out char letter
                             , out SqlInt64 total_mb
                             , out SqlInt64 free_mb
                             , out string format
                             , out string type
                             , out string volume_label)
    {
        DriveInfo drive = (DriveInfo)obj;
        letter = drive.Name[0];
        type = drive.DriveType.ToString();
 
        // Some drives might be empty and thus not IsReady
        if (drive.IsReady)
        {   total_mb = new SqlInt64(drive.TotalSize / 1048576);
            free_mb = new SqlInt64(drive.TotalFreeSpace / 1048576);
            format = drive.DriveFormat;
            volume_label = drive.VolumeLabel;
        }
        else
        {   total_mb = new SqlInt64();
            free_mb = new SqlInt64();
            format = null;
            volume_label = null;
        }
    }
};