WARNING: This is a technical programming article and is quite capable of boring you to tears and/or sending you into a coma. You have been warned.
At work, I am writing quite a chunky Windows Forms application. It's starting to get out of hand, and my inadequacies with GitHub and version control are hardly helping.
Due to privacy and security concerns, I can't share much of it, besides the necessary revealing of a couple of the remote devices I communicate with which contain generic names.
...'everyone uses that same bloody Visual Studio Code picture. As I use this editor myself I thought I'd create my own, but with fingerprints and dust included'...
I am currently working in the medical arena and these devices are prefixed 'VD' for venereal disease. It fits perfectly as my application will ultimately aid people who shag around a lot and get the clap often.
These computers are riddled with the likes of Syphilis, Gonorrhea, Chlamydia (aka 'The Clap'), and many other lovely diseases designed to irritate your groin areas and cause you to scratch fervently all day.
What I wanted was the perfect Windows Explorer icon; the remote version with that green pipe. After some digging, I discovered this icon to be in the file ‘C:\Windows\System32\imageres.dll’
I found it easily the first time around. This time I was getting eyestrain, many icons look the same and all I can say is this resource file is big.
Finding it is the first challenge, but you then need to extract it and guess at the correct size. These range from 16x16 pixels to 320x320 on occasion. You might think it may be best to grab the largest size and then scale it down and yes, I tried this.
The result was an icon that didn’t quite have the correct shading. I was being a pedantic bastard and wanting to find a cure for Syphilis faster, it needed to look good. My balls were starting to itch thinking about it all.
I settled on 48x48 after sizing up the icon with what I could see in Windows Explorer. It looked about right and that was the first step.
Next was the font. I tried ChatGpt and asked it what font Microsoft uses for their Windows Explorer icon textual description. It told me they do not disclose those facts but it's probably Segoe UI, 9pt. Very fucking useful.
...'if you have not guessed already, my iteration is the top one. Close but not perfect'...
That's the default VB.Net font and it looked similar, so far so good. My font looks a little more spaced and a little less bold. Adding Bold made it over the top, that's not the answer.
I figured it was a grey background versus a white background that caused theirs to stand out more.
The font on the statistics looked the same, but the colour was greyer. Guessing their colours is trial and error. I tried a few and settled on simple 'GrayText'
Next was that thing that looks like a progress bar. Why not use the default Progress Bar Control? It seemed to do the job well and besides some stupid spacing limitations on the top edge, it looks almost the same.
The colour of it was the next problem. No amount of googling was telling me what it was. I settled on Dodger Blue, though I am not convinced. Mine looks a little brighter than the real thing.
frm_main.prg_drivespace.ForeColor = Color.DodgerBlue
If you're going to display the icon then you need some code to do the donkey work. As the Remote Registry Service is disabled at work, I had to rely on what was enabled, namely PS Remoting.
If you don't know what this is, then it gives you the ability to run Powershell scripts as if they were running on a remote device. Powerful stuff and something I have not had the privilege to use until now.
Powershell is shit for forms as it's a shell scripting language, so I designed the User Interface in VB.Net Forms, intending to shell out to Powershell for all the dirty work.
Function GetDriveSpace(ScriptLocation As String, Script As String, Device As String) As Boolean
' Calls the Powershell script, 'GetDriveSpace.ps1'
Dim ScriptPath As String = ScriptLocation & "\" & Script
Dim StartInfo As New ProcessStartInfo()
StartInfo.FileName = "powershell.exe"
StartInfo.Arguments = $"-ExecutionPolicy Bypass -File ""{ScriptPath}"" " & $"{Device}"""
StartInfo.UseShellExecute = False
StartInfo.RedirectStandardOutput = True
StartInfo.CreateNoWindow = True
Dim MyProcess As Process = Process.Start(StartInfo)
Dim OutData As String = MyProcess.StandardOutput.ReadToEnd()
MyProcess.WaitForExit()
If MyProcess.ExitCode = 0 Then
Dim Separator() As Char = {vbCr, vbLf}
DataLines = OutData.Split(Separator, StringSplitOptions.RemoveEmptyEntries)
Return True
Else
Return False
End If
End Function
Creating a ProcessStartInfo object, I noticed one very useful property, namely the RedirectStandardOutput Boolean which when set to True redirects output using the .StandardOutput.ReadToEnd() method.
So anything sent to the screen goes into an String variable which I can process to eradicate any shitty spaces and bullshit characters ending up with just a couple of strings, namely $FreeSpaceGB and $DriveSizeGB.
In Powershell, if you just enter variables in your script then they are sent to the console. I read this is common practice and after using some other frankly other crappy method, I was delighted at discovering this.
param([string]$RemoteComputer)
$DriveLetter = "C:"
$Session = New-PSSession -ComputerName $RemoteComputer
$DriveInfo = Invoke-Command -Session $session -ScriptBlock { Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='$using:DriveLetter'" }
Remove-PSSession -Session $Session
$DriveSizeGB = [math]::Round($DriveInfo.Size / 1GB, 2)
$FreeSpaceGB = [math]::Round($DriveInfo.FreeSpace / 1GB, 2)
$FreeSpaceGB
$DriveSizeGB
The GetDriveSpace.ps1 script accepts a parameter of the remote computer, gets the space data, writes it to a custom class, and sends it back to the main VB.Net form where I can intercept it and stick those values next to that icon I have painstakingly been trying to ripoff!
I noted the wording on the explorer icon. The GB free is always to two decimal points. This is taken care of in the Powerscript here:
Sub ShowSpaceStatistics()
' Makes the Drivespace, Drivename labels and Drivespace Progress bar, visible.
' Calculates the Percentage of drive space left, and changes the colour to red if lower than 10% free space
' Displays the Drive logo and data to the user
frm_main.pic_remotedrive.Visible = True
frm_main.pic_remotedrive.Image = My.Resources.remotedrive_48x_48x
frm_main.lbl_drivename.Visible = True
frm_main.lbl_drivespace.Visible = True
frm_main.prg_drivespace.Visible = True
frm_main.prg_drivespace.Maximum = DataLines(1)
frm_main.lbl_drivename.Text = "c$ (\\" & RemoteDevice & ") (IPC$)"
frm_main.lbl_drivespace.Text = DataLines(0) & " GB free of " & DataLines(1) & " GB"
Dim Percentage As Double = (DataLines(0) / DataLines(1)) * 100
If Percentage < 10 Then
frm_main.prg_drivespace.ForeColor = Color.Red
Else
frm_main.prg_drivespace.ForeColor = Color.DodgerBlue
End If
Dim Spaceleft As Double = Math.Round(DataLines(1) - DataLines(0), 2)
frm_main.prg_drivespace.Value = Spaceleft
frm_main.prg_drivespace.Maximum = DataLines(1)
End Sub
Then there's the colour of the progress bar that needs to turn RED when the drive is low on space. Googling this again, I found it to be under 10% free and it's no longer BLUE. Not an issue and is addressed here.
If Percentage < 10 Then
frm_main.prg_drivespace.ForeColor = Color.Red
Else
frm_main.prg_drivespace.ForeColor = Color.DodgerBlue
End If
Finally, I needed the HDD space-free value, which is simply a calculation of the total space – the used space, again to two decimal points.
Dim Spaceleft As Double = Math.Round(DataLines(1) - DataLines(0), 2)
Something as simple as recreating the HDD icon with true statistics takes some effort, and though hardly difficult for me, it needed to some tenacity to track down everything I needed.
Oh, and if you believed me about all that Venereal Disease bullshit, then what can I say? My application CURRENTLY only helps people with Trichomoniasis, and I'm adding 'The Clap' support next week.