How to get free Drive Space in C# and VB.NET

To get free Drive Space in C# and VB.NET you can use the following snippet.

Sample C#

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,out ulong lpFreeBytesAvailable,out ulong lpTotalNumberOfBytes,out ulong lpTotalNumberOfFreeBytes);

public static bool GetFreeDriveSpace(string path, out ulong freebytes)
{
	if (string.IsNullOrEmpty(path))
	{
		throw new ArgumentNullException("path");
	}

	if (!path.EndsWith("\\"))
	{
		path = String.Concat(path, "\\");
	}

	ulong lpFreeBytesAvailable;
	ulong lpTotalNumberOfBytes;
	ulong lpTotalNumberOfFreeBytes;

	if (GetDiskFreeSpaceEx(path, out lpFreeBytesAvailable, out lpTotalNumberOfBytes, out lpTotalNumberOfFreeBytes))
	{
		freebytes = lpFreeBytesAvailable;
		return true;
	}
	freebytes = 0;
	return false;
}

Sample VB.NET

<DllImport("kernel32.dll", SetLastError := True, CharSet := CharSet.Auto)> _
Private Shared Function GetDiskFreeSpaceEx(lpDirectoryName As String, ByRef lpFreeBytesAvailable As ULong, ByRef lpTotalNumberOfBytes As ULong, ByRef lpTotalNumberOfFreeBytes As ULong) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

Public Shared Function GetFreeDriveSpace(path As String, ByRef freebytes As ULong) As Boolean
	If String.IsNullOrEmpty(path) Then
		Throw New ArgumentNullException("path")
	End If

	If Not path.EndsWith("\") Then
		path = [String].Concat(path, "\")
	End If

	Dim lpFreeBytesAvailable As ULong
	Dim lpTotalNumberOfBytes As ULong
	Dim lpTotalNumberOfFreeBytes As ULong

	If GetDiskFreeSpaceEx(path, lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes) Then
		freebytes = lpFreeBytesAvailable
		Return True
	End If
	freebytes = 0
	Return False
End Function

Please rate this snippet

How to use the Space() function in MSSQL

To learn how to use the Space() function in MSSQL you can take a look at the following snippet.

 

Let’s say you have a table (tblTest) like this:

ID Testcolumn
13 X000013
14 X000014
15 X000015

 

You want to select the next “X” number for Testcolumn. The problem is that when adding something to 000015 you will loose the 0 characters. So after getting the information that 16 is the next highest number you need to fill the gap between X and 16 with 0. And if the length of the X number is not fixed, you need to do that generic. The Space() function can help you alot. What the space function does is, it just creates as much spaces as you need. We will use this to replace the Spaces then with 0.

Sample MSSQL

SELECT TOP 1 Testcolumn AS [Currently Highest Field Value],

CONCAT(
	LEFT(Testcolumn,1), -- select X
	REPLACE( --Replace *
	SPACE(LEN(Testcolumn) - 1 - LEN(CONVERT(BIGINT,SUBSTRING(Testcolumn,2,LEN(Testcolumn))))) -- adds the missing characters as Space
	,' ','0') -- * each created SPACE() with a 0
	, 
	CONVERT(BIGINT,SUBSTRING(Testcolumn,2,LEN(Testcolumn))) --select 15
	+1 -- adds 1
	) AS  VALUE
    
FROM tblTest 
ORDER BY SUBSTRING(testcolumn,2,LEN(testcolumn)) DESC

The result will be

X000016

Please rate this snippet

How to get free disk space in C# and VB.NET

To get free disk space in C# and VB.NET you can use the following snippet.

Sample C#

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes,out ulong lpTotalNumberOfFreeBytes);

public static bool GetFreeBytes(string folderName, out ulong freeBytes)
{
	freeBytes = 0;
	if (!folderName.EndsWith("\\"))
	{
		folderName += '\\';
	}
	ulong freeSpaceInBytes = 0;
	ulong notUsed = 0;
	ulong notUsed2 = 0;
	if (GetDiskFreeSpaceEx(folderName, out freeSpaceInBytes, out notUsed, out notUsed2))
	{
		freeBytes = freeSpaceInBytes;
		return true;
	}
	else
	{
		return false;
	}
}

Sample VB.NET

<DllImport("kernel32.dll", SetLastError := True, CharSet := CharSet.Auto)> _
Public Shared Function GetDiskFreeSpaceEx(lpDirectoryName As String, ByRef lpFreeBytesAvailable As ULong, ByRef lpTotalNumberOfBytes As ULong, ByRef lpTotalNumberOfFreeBytes As ULong) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

Public Shared Function GetFreeBytes(folderName As String, ByRef freeBytes As ULong) As Boolean
	freeBytes = 0
	If Not folderName.EndsWith("\") Then
		folderName += "\"
	End If
	Dim freeSpaceInBytes As ULong = 0
	Dim notUsed As ULong = 0
	Dim notUsed2 As ULong = 0
	If GetDiskFreeSpaceEx(folderName, freeSpaceInBytes, notUsed, notUsed2) Then
		freeBytes = freeSpaceInBytes
		Return True
	Else
		Return False
	End If
End Function

Please rate this snippet