VB .NET-如何检查是否'文件和打印机共享'已启用

发布于 2025-02-10 04:15:42 字数 721 浏览 0 评论 0 原文

我已经写了一个VB .NET前端到Office SQL Server。我想扩展此信息,以便用户可以通过VPN访问服务器。在测试过程时,如果我在每个客户端PC上还启用了“文件和打印机共享”,我只能通过VPN查看SQL Server,因此我希望一些代码在尝试之前是否启用了VPN和共享访问服务器。我有以下三行检查VPN正在运行:

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

但是我找不到(在VB .NET中)检查客户端PC是否启用了“文件和打印机共享”。我发现了这篇文章 1 讨论使用PowerShell进行检查,但我没有知道如何将其转换为VB .NET代码(或这是正确 /最佳方法)。 Office网络不是一个域,因此我无法通过小组策略启用共享。

还有其他人解决这个问题吗? 谢谢, 蒂姆

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杆小烟枪 2025-02-17 04:15:42

以下显示了如何使用vb.net中的powershell启用文件和打印机共享。

下载/安装powerShell的nuget软件包

  • microsoft.powershell.5.1.ReferenceAssemblies (.net Framework)
  • microsoft.powershell.powershell.sdk (.NET 6)(.NET 6)

请参阅此 post 有关更多信息。


添加以下导入语句

  • imports system.Management.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

其他资源

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文