0

I have written a VB .Net front-end to the office SQL server. I want to expand this so users can access the server via a VPN. When testing the process, I can only see the SQL server via the VPN if I also have 'File and Printer Sharing' enabled on each client PC, so I want some code to check that both the VPN and the sharing are enabled before trying to access the server. I have the following three lines which checks that the VPN is running:

    Dim myInterfaceList As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces
    Dim myVPNRunning As Boolean = myInterfaceList.AsEnumerable().Any(Function(x) x.Name = "MyTestVPN")
    If myVPNRunning Then chkProgress3.Checked = True

but I can't find a way (in VB .NET) to check if the client PC has 'File and Printer Sharing' enabled. I have found this article 1 which discusses using Powershell to check, but I don't know how to translate this into VB .Net code (or even if this is the correct / best way). The office network is not a Domain, so I can't enable sharing through Group Policy.

Has anyone else tackled this? Thanks, Tim

  • Rather than providing direct access to the database you may consider creating an API (ie: middleware) which authenticates the users and handles the database communication. – Tu deschizi eu inchid Jun 22 '22 at 16:22
  • For getting info using PowerShell, the following may be helpful: https://stackoverflow.com/a/72651882/10024425 – Tu deschizi eu inchid Jun 22 '22 at 16:23
  • Does this answer your question? [File and Printer Sharing Status](https://stackoverflow.com/questions/48787845/file-and-printer-sharing-status) – Fawlty Jun 22 '22 at 16:25
  • Thanks user9938, but I think that an API is overkill for my requirements. Just wanted to check the VPN was active before I tried to use it. Thanks anyway, may have a look down the line – Tim Hutchinson Jun 24 '22 at 08:57

1 Answers1

1

The following shows how to check if File and Printer sharing is enabled using PowerShell in VB.NET.

Download/install NuGet package for PowerShell

  • Microsoft.PowerShell.5.1.ReferenceAssemblies (.NET Framework)
  • Microsoft.PowerShell.SDK (.NET 6)

See this post for more information.


Add the following Imports statements:

  • Imports System.Management.Automation

IsFileAndPrinterSharingEnabled:

Private Function IsFileAndPrinterSharingEnabled() As Boolean
    Using ps As PowerShell = PowerShell.Create()
        Dim result As String = ps.AddScript("Get-NetAdapterBinding | ? {$_.DisplayName -eq 'File and Printer Sharing for Microsoft Networks' -and $_.Name -eq 'ethernet'} | Select -ExpandProperty Enabled").Invoke()(0).ToString()
        'Dim result As String = ps.AddScript("Get-NetAdapterBinding | ? {$_.DisplayName -eq 'File and Printer Sharing for Microsoft Networks' -and $_.Name -eq 'ethernet'} | ForEach-Object {$_.Enabled}").Invoke()(0).ToString()

        If result = "True" Then
            Return True
        End If
    End Using

    Return False
End Function

Resources:

Other Resources

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
  • What would the purpose of the commented-out line `'Dim result As String = ...` be? – Andrew Morton Jun 22 '22 at 21:32
  • @AndrewMorton: A slightly different command that one can use adapted from [Output data with no column headings using PowerShell](https://stackoverflow.com/questions/1408042/output-data-with-no-column-headings-using-powershell). – Tu deschizi eu inchid Jun 22 '22 at 21:41