Using the jpegPhoto attribute in AD - Part II

The previous post (https://weblogs.asp.net/btrst4/archive/2004/08/30/222629.aspx) on this topic showed you how to import picture data into AD. This post shows you how to retieve the data and write it back out to a file. It might be more realistic to retrieve the data from AD and display it in a portal. I'm sure someone could adapt this code to do this quite easily.  If you know how, please post or email me.

Basically, this is the reverse of the previous code using System.DirectoryServices then System.IO. 

Here is the code:
Imports System.IO
Imports System.DirectoryServices

Module Module1
Sub Main()
Dim outFile As System.IO.FileStream
Dim binaryData() As Byte
Dim strFileName As String

        strFileName = "c:\PictureExportedFromAD.jpg"

        'Connect to AD
Dim strDN As String = "CN=Joe User,OU=Employees,DC=company,DC=local"
Dim strDCName As String = "DC-01"
Dim myUser As New System.DirectoryServices.DirectoryEntry("LDAP://" & strDCName & "/" & strDN)

        'Retrieve picture into byte array
Dim byteArray As Byte()
byteArray = myUser.Properties("jpegPhoto").Value
myUser.Close()

        'Open file to export (delete if exists)
If File.Exists(strFileName) Then
File.Delete(strFileName)
End If

        outFile = New System.io.FileStream(strFileName, FileMode.CreateNew, FileAccess.ReadWrite)

        'Loop through bytes and write to file
Dim singleByte As Byte

        For Each singleByte In byteArray
outFile.WriteByte(singleByte)
Next
outFile.Close()
End Sub
End Module