Category Archives: Visual Basic

Get files in folder and the date in vb.net

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

            Next

Get Current logged on Username – vb.net – 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

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
        AxMsRdpClient8NotSafeForScripting1.Connect()
        
        ''now resize
        AxMsRdpClient8NotSafeForScripting1.Height = 580
        AxMsRdpClient8NotSafeForScripting1.Width = 760
    End Sub