Andrea Bertolotto
MyMind.InvokeMember("Dump", BindingFlags.Default | BindingFlags.InvokeMethod, null, o, new object[] {"0xD3F3C+00"});
Skip Navigation LinksHome > Articles > Free Space Tester rss  

Skip Navigation Links.
   
Free Space Tester 1.2.3.0

11/21/2004
Download  Download (31 KB)

Background

This small utility was conceived because some of the servers in my network, sometimes are lacking disk space. Being not able to buy new HDs , I decided to write some code to check situation, so I can act as soon as possible and free some space. Having a server with no more space on disk is a very nasty condition and everyone should be alerted before a crash could happen.This was created as a console application so it can run without user intervention, and eventually scheduled using O.S. scheduler.

Usage

Application can be configured using two files: "FreeSpaceTester.exe.config" and "Drives.txt".
This is a typical configuration file:
<configuration>
    <appSettings>
        <add key="Threshold" value="1024"/>
        <add key="ConsoleOutput" value="true"/>
        <add key="MailTo" value="me@mail.com"/>
        <add key="MailFrom" value="freespacetester@myserver.com"/>
        <add key="MailServer" value="mail.myserver.com"/>
    </appSettings>
</configuration>
Where:
Threshold - If free space on target drive is less than this value (in megabytes) alert mails will be sent
ConsoleOutput - Boolean value indicating if report is built on execution and printed out
MailTo - E-mail addresses of alert mails (multiple addresses can be separated using semi-colon)
MailFrom - E-mail address of sender
MailServer - SMTP server used for sending mails

On the other side this is a typical configuration drive list:
C:
\\URANUS\C$
\\NEPTUNE\D$
Where C is a local drive and other ones are remote ones. You should have administrative rights to those servers, or you would not get drive statistics. In that case you would get an "Unable to get data" message.


Implementation

The hard job is done by Windows API "GetDiskFreeSpaceEx":
[DllImport("kernel32", CharSet=CharSet.Auto)]
static extern int GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
A reference to API can be found here: GetDiskFreeSpaceEx on MSDN

A very basic data formatting is provided, to show byte numbers using appropriate unit measures. This is the routine:
public static string FormatSize(double size)
{
    if (size <= 0)
        return "0 bytes";
    else if (size < kb)
        return size.ToString() + " bytes";
    else if ((size >= kb) && (size < mb))
        return Math.Round(size / kb, 0).ToString() + "
            KB";
    else if ((size >= mb) && (size < gb))
        return Math.Round(size / mb, 0).ToString() + "
            MB";
    else if (size >= gb)
        return Math.Round(size / gb, 0).ToString() + "
            GB";
    return size.ToString() + " bytes";
}
   
My status Get Skype and call me for free.


















 
Copyright © 2004-7 - Andrea Bertolotto - Site Version: 2.3.0.0 - 2/22/2010