Drive Space Polling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Cantley

    #1

    Drive Space Polling

    I need to write an app that keeps check on our drives. I have tried using
    DirectoryInfo and Directory but it is way too slow. I have a new 2 terrabyte
    server going into production on Monday. Is there a way to just get the size,
    space used and space available, just like in the properties when looking at
    a drive?

    jc


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Drive Space Polling

    John,

    You should be able to call the GetDiskFreeSpac eEx API function through
    the P/Invoke layer. The definition is as follows:

    [DllImport("kern el32.dll", SetLastError=tr ue, CharSet=CharSet .Auto)]
    public static extern bool GetDiskFreeSpac eEx(
    [MarshalAs(Unman agedType.LPTStr )] string lpDirectoryName ,
    [MarshalAs(Unman agedType.U8)] long lpFreeBytesAvai lable,
    [MarshalAs(Unman agedType.U8)] long lpTotalNumberOf Bytes,
    [MarshalAs(Unman agedType.U8)] long lpTotalNumberOf FreeBytes);

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "John Cantley" <jcstrider@hotm ail.com> wrote in message
    news:%23r21%232 UeDHA.1712@TK2M SFTNGP09.phx.gb l...[color=blue]
    > I need to write an app that keeps check on our drives. I have tried using
    > DirectoryInfo and Directory but it is way too slow. I have a new 2[/color]
    terrabyte[color=blue]
    > server going into production on Monday. Is there a way to just get the[/color]
    size,[color=blue]
    > space used and space available, just like in the properties when looking[/color]
    at[color=blue]
    > a drive?
    >
    > jc
    >
    >[/color]


    Comment

    • John Cantley

      #3
      Re: Drive Space Polling

      Nicholas;

      Here is what I have

      public class WinApi
      {
      [DllImport("kern el32.dll", SetLastError=tr ue, CharSet=CharSet .Auto)]
      public static extern bool GetDiskFreeSpac eEx(
      [MarshalAs(Unman agedType.LPTStr )] string lpDirectoryName ,
      [MarshalAs(Unman agedType.U8)] long lpFreeBytesAvai lable,
      [MarshalAs(Unman agedType.U8)] long lpTotalNumberOf Bytes,
      [MarshalAs(Unman agedType.U8)] long lpTotalNumberOf FreeBytes);
      }

      protected void GetFreeSpace()
      {
      long lFreeBytes = 0;
      long lTotBytes = 0;
      long lTotFreeBytes = 0;
      string sDir = "C:\\";
      bool bOk = WinApi.GetDiskF reeSpaceEx(sDir , lFreeBytes, lTotBytes,
      lTotFreeBytes);
      if (bOk)
      Console.Write(l FreeBytes.ToStr ing());
      }

      I don't get any values for anything, am I missing another step?

      jc
      [color=blue]
      > You should be able to call the GetDiskFreeSpac eEx API function through
      > the P/Invoke layer. The definition is as follows:
      >
      > [DllImport("kern el32.dll", SetLastError=tr ue, CharSet=CharSet .Auto)]
      > public static extern bool GetDiskFreeSpac eEx(
      > [MarshalAs(Unman agedType.LPTStr )] string lpDirectoryName ,
      > [MarshalAs(Unman agedType.U8)] long lpFreeBytesAvai lable,
      > [MarshalAs(Unman agedType.U8)] long lpTotalNumberOf Bytes,
      > [MarshalAs(Unman agedType.U8)] long lpTotalNumberOf FreeBytes);
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - nicholas.paldin o@exisconsultin g.com
      >
      > "John Cantley" <jcstrider@hotm ail.com> wrote in message
      > news:%23r21%232 UeDHA.1712@TK2M SFTNGP09.phx.gb l...[color=green]
      > > I need to write an app that keeps check on our drives. I have tried[/color][/color]
      using[color=blue][color=green]
      > > DirectoryInfo and Directory but it is way too slow. I have a new 2[/color]
      > terrabyte[color=green]
      > > server going into production on Monday. Is there a way to just get the[/color]
      > size,[color=green]
      > > space used and space available, just like in the properties when looking[/color]
      > at[color=green]
      > > a drive?
      > >
      > > jc
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Willy Denoyette [MVP]

        #4
        Re: Drive Space Polling

        Just take a look at the Management classes (WMI) you can find a way to trigger events when a certain threshold has been reached.

        Willy.

        "John Cantley" <jcstrider@hotm ail.com> wrote in message news:%23r21%232 UeDHA.1712@TK2M SFTNGP09.phx.gb l...[color=blue]
        > I need to write an app that keeps check on our drives. I have tried using
        > DirectoryInfo and Directory but it is way too slow. I have a new 2 terrabyte
        > server going into production on Monday. Is there a way to just get the size,
        > space used and space available, just like in the properties when looking at
        > a drive?
        >
        > jc
        >
        >[/color]


        Comment

        Working...