December 15, 2007
@ 04:15 PM

If you need to convert a Bitmap to an Byte array, here is how you do it.

  Public Shared Function ConvertToByteArray(ByVal value As Bitmap) As Byte()

      Dim bitmapBytes As Byte()

 

      Using stream As New System.IO.MemoryStream

 

        value.Save(stream, value.RawFormat)

        bitmapBytes = stream.ToArray

 

      End Using

 

      Return bitmapBytes

 

  End Function

Tip By: David McCarter

This code can be found in the open source dotNetTips.Utility assembly


 
Categories: VB.NET

There are many times when I need to convert an array to a delimited string. Here is an easy way to do it using generics in .NET 2.0.

  Public Shared Function ConvertArrayToString(Of arrayType As {IEnumerable})(ByVal array As arrayType(), ByVal separator As Char) As String

 

      If Not IsValidArray(array) Then

        Throw New ArgumentNullException("array")

      End If

 

      If IsNothing(separator) Then

        Throw New ArgumentNullException("separator")

      End If

 

      Dim result As New System.Text.StringBuilder

 

      For Each arrayItem As arrayType In array

        If result.Length > 0 Then

          result.Append(separator)

        End If

 

        result.Append(arrayItem.ToString)

 

      Next

 

      Return result.ToString

 

  End Function

Tips By: David McCarter

This code can be found in the open source dotNetTips.Utility assembly


 
Categories: VB.NET

December 14, 2007
@ 12:44 PM

.NET still does not expose the drive serial number via the framework. For that you will need to use WMI. Here is the code.

Shared Function GetDriveSerialNumber(ByVal drive As String) As String

    Dim driveSerial As String = String.Empty

 

    'No matter what is sent in, get just the drive letter

    Dim driveFixed As String = System.IO.Path.GetPathRoot(drive)

    driveFixed = Replace(driveFixed, "\", String.Empty)

 

    'Perform Query

    Using querySearch As New ManagementObjectSearcher("SELECT VolumeSerialNumber FROM Win32_LogicalDisk Where Name = '" & driveFixed & "'")

      Using queryCollection As ManagementObjectCollection = querySearch.Get()

        Dim moItem As ManagementObject

        For Each moItem In queryCollection

          driveSerial = CStr(moItem.Item("VolumeSerialNumber"))

          Exit For

        Next

      End Using

    End Using

 

    Return driveSerial

 

End Function

You will also need to reference System.Management in your project.

Tips By: David McCarter

This code can be found in the open source dotNetTips.Utility assembly


 
Categories: VB.NET

December 14, 2007
@ 12:33 PM

Here is some simple code to validate that an array is valid. Before trying to use an array, you should always validate it with this code first.

  Public Shared Function IsValidArray(ByVal array As Array) As Boolean

      Dim valid As Boolean = False

 

      If array Is Nothing Then

        Throw New ArgumentNullException("array")

      End If

 

      If (array IsNot Nothing) AndAlso (array.Length > 0) Then

        valid = True

      End If

 

      Return valid

 

  End Function

Tip By: David McCater

This code can be found in the open source dotNetTips.Utility assembly


 
Categories: Defensive Programming | VB.NET

When data binding to a DropDownList control in ASP.NET, there are in many cases when you want the first selection of the control to be blank or say something like "<please select value>". In part one of this tip (back in 2003), I wrote that you could just add an empty row to the DataSet. In VS 2005 and the new way of data binding that I usually do to web services, this is more difficult. So here is another solution just using code in your ASP.NET code.

 Protected Sub DropDownList_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyControl1.DataBound, MyControl2.DataBound
      DirectCast(sender, DropDownList).Items.Insert(0, String.Empty)
 End Sub

The Insert method must be called after the DropDownList has been data bound.

Tip Submitted By: David McCarter


 
Categories: ASP.NET | VB.NET