0

Throwback on an old project and needing to use VB6. I'm having an issue in referencing the appropriate DLL that contains System.IO in the old VB6 IDE.

I have tried to reference: C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll - error: Can't add reference to specified file

Added a reference to C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.tlb - doesn't work.

Added reference to C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.tlb - there is no System.IO from the intellisense.

enter image description here

Can someone please post the step-by-step instructions?

ElHaix
  • 12,846
  • 27
  • 115
  • 203
  • 1
    You can't. Framework classes don't expose a COM interface. You can either use the included VB6 file system objects and interfaces, or call Win api, or if you really want to use Framework classes, you'll need to code a (small?) .net app that exposes a COM Interop, call that from your VB6 app, and then make the Framework calls in there on behalf of your VB6 app. – MarkL Aug 17 '18 at 15:56
  • So how do I get access to the disk space free/used methods? – ElHaix Aug 17 '18 at 16:04
  • Alternatively I want to use My.Computer.FileSystem... unable to locate where to reference this/ – ElHaix Aug 17 '18 at 16:44
  • 1
    There is nothing in the .NET framework that you can use directly. VB6 was not based on .NET in any way, and predated it by years. – StayOnTarget Aug 20 '18 at 11:33

1 Answers1

0

Got this somewhat working with:

Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) As Long

Dim info As DiskInformation
Dim lAnswer As Long
Dim lpRootPathName As String
Dim lpSectorsPerCluster As Long
Dim lpBytesPerSector As Long
Dim lpNumberOfFreeClusters As Long
Dim lpTotalNumberOfClusters As Long
Dim lBytesPerCluster As Long
Dim lNumFreeBytes As Double
Dim dPercentFreeClusters As Double
Dim sString As String

lpRootPathName = "c:\"
lAnswer = GetDiskFreeSpace(lpRootPathName, lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters)
lBytesPerCluster = lpSectorsPerCluster * lpBytesPerSector

' Throws overflow exception - I guess there were no Terabyte drives when VB6 came around
' lNumFreeBytes = lBytesPerCluster * lpNumberOfFreeClusters

'sString = "Number of Free Bytes : " & lNumFreeBytes & vbCr & vbLf
'sString = sString & "Number of Free Kilobytes: " & (lNumFreeBytes / 1024) & "K" & vbCr & vbLf
'sString = sString & "Number of Free Megabytes: " & Format(((lNumFreeBytes / 1024) / 1024), "0.00") & "MB"

dPercentFreeClusters = Round(lpNumberOfFreeClusters / lpTotalNumberOfClusters * 100, 2)

There is, however, an Overflow exception being thrown when trying to calculate the number of free bytes.

I would like to get this working with My.Computer.FileSystem. Suggestions?

ElHaix
  • 12,846
  • 27
  • 115
  • 203
  • VB6 longs are 32bit signed quantities (2^31-1 is the max). The overflow would be occurring on multiplying two longs together, even though you're assigning this result to a Double. You'll need to calc with floating point variables (double or single) to avoid this, ie: Dim lBytesPerCluster As Double and Dim lpNumberOfFreeClusters As Double. Alternatively you could probably utilize Decimal variables instead of Double if fixed decimal quantities are required (floating points will drop the least significant digits when the values get large enough, of course). – MarkL Aug 17 '18 at 19:11
  • Correction to my comment above - VB6 doesn't have a Decimal variable type, of course. I should have said, and meant to say, to use the Currency variable type. – MarkL Aug 17 '18 at 19:22