/*********************************************************************************************
 * Copyright 2005 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: DirSpace.cs $     $Revision: 1 $
 * $Author: Kathy $   $Date: 3/08/05 1:46p $
 *
 * $History: DirSpace.cs $
 * 
 * *****************  Version 1  *****************
 * User: Kathy        Date: 3/08/05    Time: 1:46p
 * Created in $/LibSource/Utils
 * Approval
 *********************************************************************************************/
using System;
using System.IO;
using System.Runtime.InteropServices;

namespace Utils
{
	/// <summary>
	/// Summary description for DirSpace.
	/// </summary>
	public class DirSpace
	{
		static DirSpace()
		{			
		}

		[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
		public static extern bool GetDiskFreeSpaceEx(
			[MarshalAs(UnmanagedType.LPTStr)] string lpDirectoryName,
			ref long lpFreeBytesAvailable,
			ref long lpTotalNumberOfBytes,
			ref long lpTotalNumberOfFreeBytes);
		
		public static long Free()
		{
			string curdir = Environment.CurrentDirectory;
			long freeBytes = new long();
			long totalFreeBytes = new long();
			long totalBytes = new long();

			bool x = GetDiskFreeSpaceEx(curdir, ref freeBytes, ref totalFreeBytes, ref
				totalBytes);
			return totalFreeBytes;
		}

		private static long DirSize(DirectoryInfo d) 
		{    
			long Size = 0;    
			// Add file sizes.
			FileInfo[] fis = d.GetFiles();
			foreach (FileInfo fi in fis) 
			{      
				Size += fi.Length;    
			}
			// Add subdirectory sizes.
			DirectoryInfo[] dis = d.GetDirectories();
			foreach (DirectoryInfo di in dis) 
			{
				Size += DirSize(di);   
			}
			return(Size);  
		}

		public static long Get(string TheDirectory)
		{
			DirectoryInfo d = new DirectoryInfo(TheDirectory);
			return DirSize(d);
		}
	}
}
