Skip to main content

Working with Dates - 1

Scenario: You need to fit a dentist appointment into an already-packed schedule. The Dentist has given you a list of dates this month where he can fit you in. Use the following query to determine which dates work for both of you.

Public Sub Linq107()
    Randomize()

    Dim y = Year(Now)
    Dim m = Month(Now)

    'Randomly pick 15 appointment dates this month
    Dim DentistDates(15) As Date, AvailableDates(8) As Date
    For i = 0 To UBound(DentistDates)
        DentistDates(i) = New Date(y, m, Int(Date.DaysInMonth(y, m) * Rnd() + 1))
    Next

    'Randomly pick 8 dates you're free this month
    For i = 0 To UBound(AvailableDates)
        AvailableDates(i) = New Date(y, m, Int(Date.DaysInMonth(y, m) * Rnd() + 1))
    Next

    Dim CommonDates = From _
             d In AvailableDates, d2 In DentistDates _
        Where d.Date = d2.Date _
        Select d _
        Distinct _
        Order By d

    'Just to make it easier to see that the query works, we sort the original lists before display
    Array.Sort(DentistDates)
    Array.Sort(AvailableDates)

    Console.WriteLine("Dentist is free on:")
    ObjectDumper.Write(DentistDates)

    Console.WriteLine(vbNewLine & "You are free on:")
    ObjectDumper.Write(AvailableDates)

    Console.WriteLine(vbNewLine & "Appointment dates that work:")
    For Each d In CommonDates
        Console.WriteLine(Format(d, "MM/dd/yy"))
    Next
End Sub


Result:
Dentist is free on:
8/1/2007
8/2/2007
8/2/2007
8/6/2007
8/10/2007
8/11/2007
8/12/2007
8/13/2007
8/14/2007
8/17/2007
8/23/2007
8/25/2007
8/26/2007
8/28/2007
8/31/2007
8/31/2007

You are free on:
8/1/2007
8/2/2007
8/8/2007
8/10/2007
8/12/2007
8/15/2007
8/20/2007
8/28/2007
8/29/2007

Appointment dates that work:
08/01/07
08/02/07
08/10/07
08/12/07
08/28/07


System.Diagnostics

This query determines the top 5 memory-using applications currently loaded.

Public Sub Linq108()

    Dim pList = From p In System.Diagnostics.Process.GetProcesses() _
                Select ProcessName = p.ProcessName, _
                       Size = (Format(p.WorkingSet64 / 1000, "#,##0") & " KB"), _
                       Size64 = p.WorkingSet64 _
                Order By Size64 Take 5

    Console.WriteLine("These 5 processes are using the most memory:")

    For Each p In pList
        Console.WriteLine(p.ProcessName & " - " & p.Size)
    Next

End Sub


Result:
These 5 processes are using the most memory:
Idle - 16 KB
System - 217 KB
smss - 381 KB
lsass - 934 KB
vpcmap - 958 KB


Working with the File System - 1

This query finds hidden files in a given directory.

Public Sub Linq109()

    Dim fileList = From f In New DirectoryInfo("C:\").GetFiles() _
        Where (f.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden _
        Order By f.Name

    Console.WriteLine("Hidden Files:")

    For Each f In fileList
        Console.WriteLine(f.Name)
    Next
End Sub


Result:
Hidden Files:
boot.ini
IO.SYS
MSDOS.SYS
NTDETECT.COM
ntldr
pagefile.sys


Working with the File System - 2

This query finds files created within the last year.

Public Sub Linq110()

    Dim fileList = From f In New DirectoryInfo("C:\").GetFiles() _
                   Where f.CreationTime.AddYears(1) > Now _
                   Select f _
                   Order By f.Name

    Console.WriteLine("Files created within the last year:")
    For Each f In fileList
        Console.WriteLine(f.Name)
    Next

End Sub


Result:
Files created within the last year:
VB101LINQQuerySamples_Content.zip
VB101LINQQuerySamples_Content_ByCategory.zip
VB101LINQQuerySamples_Content_SeparatePages.zip


Working with the File System - 3

This query shows all mapped network drives

Public Sub Linq111()

    'If you want to see other drive types (i.e. CDRom) please change the enum below
    Dim Drives = From d In My.Computer.FileSystem.Drives() _
                 Where d.DriveType = System.IO.DriveType.Network _
                 Select d

    Console.WriteLine("Network Drives:")
    For Each d In Drives
        Console.WriteLine(d.Name)
    Next

End Sub


Result:
Network Drives:


Command Line Arguments

Uses LINQ to check for the /help command line argument.

Public Sub Linq112()
    'To experiment with this you can double-click My Project, click on Debug and
    'enter /help in the command line arguments textbox

    Dim cmdArgs = (From c In My.Application.CommandLineArgs() _
                  Where c = "/help" _
                  Select c).Count()

    If cmdArgs = 0 Then
        Console.WriteLine("""/help"" was not entered as a command line argument.")
    Else
        Console.WriteLine("""/help"" requested by user on the command line.")
    End If

End Sub


Result:
"/help" was not entered as a command line argument.


Working with the Registry - 1

Shows all keys under HKLM\Software that start with the letter C.

Public Sub Linq113()

    Dim SubKeys = _
        My.Computer.Registry.LocalMachine.OpenSubKey("Software").GetSubKeyNames

    Dim reg = From r In SubKeys _
              Where r.StartsWith("C") _
              Order By r

    ObjectDumper.Write(reg)
End Sub


Result:
C07ft5Y
Classes
Clients
ComputerAssociates


Working with the Registry - 2

Shows keys common to HKLM\Software and HKCU\Software

Public Sub Linq114()

    Dim LMKeys = _
        My.Computer.Registry.LocalMachine.OpenSubKey("Software").GetSubKeyNames

    Dim UserKeys = _
        My.Computer.Registry.CurrentUser.OpenSubKey("Software").GetSubKeyNames

    'Performs an intersection on the two arrays
    Dim reg = From LocalMachineKey In LMKeys, UserKey In UserKeys _
              Where LocalMachineKey = UserKey _
              Select LocalMachineKey, UserKey _
              Order By LocalMachineKey

    Console.WriteLine("Keys common to HKLM\Software and HKCU\Software:")

    For Each r In reg
        Console.WriteLine(r.LocalMachineKey)
    Next
End Sub


Result:
Keys common to HKLM\Software and HKCU\Software:
Classes
Microsoft
ODBC
Policies


Recent Document History

Shows shortcuts to everything under "My Recent Documents"

Public Sub Linq115()

    Dim RecentPath As String = _
        Environment.GetFolderPath(Environment.SpecialFolder.Recent)

    'Uses an anonymous type to reduce the amount of information returned
    Dim Recent = From r In New DirectoryInfo(RecentPath).GetFiles _
                 Select r.Name, r.LastAccessTime

    Console.WriteLine("Shortcuts to My Recent Documents:")

    For Each r In Recent
        Console.WriteLine("{0,-45}{1}", r.Name, r.LastAccessTime)
    Next
End Sub


Result:
Shortcuts to My Recent Documents:
Desktop.ini 8/13/2007 9:04:44 PM
LINQQuerySamples_aggregateoperators.html.lnk 8/13/2007 10:25:29 PM
LINQSamples.lnk 8/13/2007 10:40:05 PM
LINQtoSQLSamples_insertupdatedelete.html.lnk 8/13/2007 10:25:14 PM
TOC.html.lnk 8/13/2007 10:40:05 PM


IE Favorites - 1

Counts how many items are in your Favorites folder

Public Sub Linq116()

    Dim FavPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)

    Dim FavSize = (From f In New DirectoryInfo(FavPath).GetFiles() _
                   Select f).Count()

    Console.WriteLine("There are {0} files in your Favorites folder.", FavSize)
End Sub


Result:
There are 3 files in your Favorites folder.