Category Archives: VB.NET

Get files in folder and the date in

I’m using this in a project that monitor files on a server folder.
This folder contains any failures that have not processed on the server.
I then display this on a screen with the filename and date to make sure we know what it is and the time it happened.

           Dim files() As String = IO.Directory.GetFiles("X:\Path\To\Files")

            For Each file As String In files

                'Get the date and time modified of this file
                FDate = IO.File.GetLastWriteTime(file)
                'tidy the filename up by removing the .ext of the name
                file = Replace(file, ".ext", "")

                testBox.Text = testBox.Text + file + " " + FDate + Environment.NewLine


Powershell w/ Exchange in – Work in Progress

I used this code to connect up to Exchange through Powershell with

However this is not the release code used in my projects as thats went missing…. – so I’m sticking this here with a view to update if / when I find my code.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim rsConfig As RunspaceConfiguration
        rsConfig = RunspaceConfiguration.Create()
        Dim snapInException As PSSnapInException = Nothing
        Dim info As PSSnapInInfo
        info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", snapInException)
        Dim myRunSpace As Runspace
        myRunSpace = RunspaceFactory.CreateRunspace(rsConfig)
        Dim pipeLine As Pipeline
        pipeLine = myRunSpace.CreatePipeline()
        Dim myCommand As New Command("Get-Command")
        Dim commandResults As System.Collections.ObjectModel.Collection(Of PSObject)
        commandResults = pipeLine.Invoke()
        For Each cmdlet As PSObject In commandResults
            Dim cmdletName As String
            cmdletName = cmdlet.Properties("Name").Value.ToString()


Get Current logged on Username – – using advapi32.dll

Note this codes about 6 years old as of 2014 – I’m sure there are better ways of doing this now but this worked at the time and is still live in one of my projects.

I needed to identify the currently logged on user running my application to make an audit file and also show only user options that the logged on user was setup for.

Here’s the code…

Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
ByRef nSize As Integer) As Integer

   Public Function GetUserName() As String
        'Gets currently logged on user - username
        Dim iReturn As Integer
        Dim userName As String
        'put the username in a string limit 50 chars
        userName = New String(CChar(" "), 50)
        'put the length of the username in an Integer iReturn
        iReturn = GetUserName(userName, 50)
        'Tidy up various formatting if required with the replace function like remove the .
        userName = userName.Replace("."c, " "c)
        'Return the username back
        GetUserName = userName.Substring(0, userName.IndexOf(Chr(0)))

    End Function

Scraping webpages and displaying data from it…

When you are working on a project to get data from a webpage but you don’t have access to the back-end database or a API to get any values out.

The workaround I use until something changes is Web scraping.

I have a project with this problem – it’s another VB.NET one.

My Scraping code goes along like this –

'Get the URL / Navigate a browser window too it (this is hidden in my project

'Convert the HTML of the page to a string using the body.outerhtml function
webPage1 = WebBrowser.Document.Body.OuterHtml

'Use the InString function to search for a term in the webPage1 string
If InStr(webPage1, "term to search") <> 0 Then
'get the position of the term to search
            StringSearch = InStr(webPage1, "term to search")
'pick some deatails out of the page using the position + x and capture the next n letters
            ScrapedText = Mid(webPage1, StringSearch + x, n)
'tidy up anything we don't like out of the returned string 
            ScrapedText = ScrapedText.Replace(".", "")
'now we have the scraped text we can use it in our app

        End If

Ta-da – workaround using scraping.

Resizing using smartsize with AxMsRdpClient (Microsoft Terminal Services Control Type Library COM)

I’ve been using the AxMsRdpClient from Microsoft Terminal Services Control Type Library COM (Imported from AxInterop.MSTSCLib) in a project through Visual Studio 2010 using VB.NET

and found the only way to re-size the RDP window via smart size is after making a connection at full size to the RDP and then forcing the size down.

This took me a good hour to work out since I can’t find any examples of this online.

I’m sharing this as it may help someone else looking to work with AxMsRdpClient

Private Sub ConnectScreen1_Click(sender As System.Object, e As System.EventArgs) Handles ConnectScreen1.Click
        ''Check if we are already connected to a RDP session - If so Return 
        If AxMsRdpClient8NotSafeForScripting1.Connected = 1 Then Return

        ''specify the server the RDP is going to connect up
        AxMsRdpClient8NotSafeForScripting1.Server = Settings.Default.RDP1
        AxMsRdpClient8NotSafeForScripting1.Height = 768
        AxMsRdpClient8NotSafeForScripting1.Width = 1024
        AxMsRdpClient8NotSafeForScripting1.ConnectingText = "Connecting..."

        ''enable smart sizing
        AxMsRdpClient8NotSafeForScripting1.AdvancedSettings7.SmartSizing = True

        ''connect up the RDP session
        ''now resize
        AxMsRdpClient8NotSafeForScripting1.Height = 580
        AxMsRdpClient8NotSafeForScripting1.Width = 760
    End Sub