Imports System.Web.Services
Imports System.IO

<WebService(Namespace := "http://tempuri.org/")> _
Public Class Service
    Inherits System.Web.Services.WebService

#Region " Web Services Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Web Services Designer.
        InitializeComponent()

        'Add your own initialization code after the InitializeComponent() call

    End Sub

    'Required by the Web Services Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Web Services Designer
    'It can be modified using the Web Services Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        'CODEGEN: This procedure is required by the Web Services Designer
        'Do not modify it using the code editor.
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

#End Region

    ' WEB SERVICE EXAMPLE
    ' The HelloWorld() example service returns the string Hello World.
    ' To build, uncomment the following lines then save and build the project.
    ' To test this web service, ensure that the .asmx file is the start page
    ' and press F5.
    '
    <WebMethod()> Public Function HelloWorld() As String
        Return Server.MapPath(Context.Request.ServerVariables.Item("script_name"))
    End Function


    ' PictureFolderPath - readonly property to return the picture
    ' folder...
    Public ReadOnly Property PictureFolderPath() As String
        Get

            ' get the full path of this asmx page...
            Dim asmxPath As String, picturePath As String
            asmxPath = _
        Server.MapPath(Context.Request.ServerVariables.Item("script_name"))

            ' step back through the string until the first "\"...
            Dim n As Integer
            For n = asmxPath.Length - 1 To n Step -1

                ' do we have a string?
                If asmxPath.Chars(n) = "\"c Then

                    ' get the service path - everything up to the "\"
                    Dim servicePath As String = asmxPath.Substring(0, n)

                    ' append the word "Pictures" to the end of the path...
                    picturePath = servicePath & "\Pictures"

                    ' now, stop the loop...
                    Exit For

                End If

            Next

            ' return the path...
            Return picturePath

        End Get
    End Property


    ' PictureFolder - property to the DirectoryInfo containing
    ' the pictures...
    Public ReadOnly Property PictureFolder() As DirectoryInfo
        Get
            Return New DirectoryInfo(PictureFolderPath)
        End Get
    End Property


    ' GetPictureFolders - return an array of the picture folders...
    <WebMethod()> Public Function GetPictureFolders() As String()

        ' get hold of the picture folder...
        Dim pictureFolder As DirectoryInfo = Me.PictureFolder

        ' get the array of subfolders...
        Dim pictureSubFolders() As DirectoryInfo = _
            pictureFolder.GetDirectories()

        ' create a string array to accommodate the names...
        Dim folderNames(pictureSubFolders.Length - 1) As String

        ' now, loop through the folders...
        Dim pictureSubFolder As DirectoryInfo, index As Integer
        For Each pictureSubFolder In pictureSubFolders

            ' add the name...
            folderNames(index) = pictureSubFolder.Name

            ' next...
            index += 1

        Next

        ' finally, return the list of names...
        Return folderNames

    End Function


    ' GetPicturesInFolder - return an array of pictures from the folder...
    <WebMethod()> Public Function GetPicturesInFolder(ByVal folderName _
        As String) As PictureInfo()

        ' get hold of the folder that we want...
        Dim pictureSubFolder As DirectoryInfo
        pictureSubFolder = _
               New DirectoryInfo(PictureFolderPath & "\" & folderName)

        ' we need to get the URL of the picture folder...
        Dim pictureFolderUrl As String, n As Integer
        pictureFolderUrl = _
            Context.Request.ServerVariables.Item("script_name")
        For n = pictureFolderUrl.Length - 1 To 0 Step -1

            ' do we have a slash?
            If pictureFolderUrl.Chars(n) = "/"c Then

                ' manipulate the URL...
                pictureFolderUrl = pictureFolderUrl.Substring(0, n) & _
                 "/Pictures"

                ' this is great, but we need an absolute url...
                pictureFolderUrl = _
                 "http://" & _
                 Context.Request.ServerVariables.Item("server_name") & _
                 pictureFolderUrl

                ' quit the loop...
                Exit For

            End If

        Next

        ' get the list of files in the subfolder...
        Dim pictureFiles() As FileInfo = pictureSubFolder.GetFiles

        ' create somewhere to put the picture infos...
        Dim pictureList(pictureFiles.Length - 1) As PictureInfo

        ' loop through each picture...
        Dim pictureFile As FileInfo, index As Integer
        For Each pictureFile In pictureFiles

            ' create a new pictureinfo object...
            Dim pictureInfo As New PictureInfo()
            pictureInfo.Name = pictureFile.Name
            pictureInfo.FolderName = folderName
            pictureInfo.URL = pictureFolderUrl & "/" & _
               folderName & "/" & pictureFile.Name
            pictureInfo.FileSize = pictureFile.Length
            pictureInfo.FileDate = pictureFile.LastWriteTime
            pictureInfo.ImageFormat = _
               pictureFile.Extension.Substring(1).ToUpper

            ' add it to the array...
            pictureList(index) = pictureInfo
            index += 1

        Next

        ' return the list of pictures...
        Return pictureList

    End Function



End Class
