简体   繁体   English

将代码段转换为C#

[英]convert code snippet to C#

VS 2008 VS 2008

I have this code snippet I found on a VB website. 我有一个在VB网站上找到的代码段。

But for some reason I am having trouble converting it to C#. 但是由于某种原因,我在将其转换为C#时遇到了麻烦。

My.Computer.Network.IsAvailable

Many thanks, 非常感谢,

using System.Net.NetworkInformation;

    internal class Program
    {
        private static void Main()
        {
            NetworkInterface.GetIsNetworkAvailable();
        }
    }

Yes, garethm is right, this class (Network) is from a VB.NET library - you need to reference the Microsoft.VisualBasic assembly if using in a C# project. 是的,garethm是正确的,此类(网络)来自VB.NET库-如果在C#项目中使用,则需要引用Microsoft.VisualBasic程序集。

Microsoft.VisualBasic.Devices.Network n = new Microsoft.VisualBasic.Devices.Network();
if (n.IsAvailable)
{
    // do stuff
}

Works for me - my network is available :). 为我工作-我的网络可用:)。

As far as how Network relates to NetworkInterface class, it depends on what you want to do next. 至于Network与NetworkInterface类的关系,取决于您下一步要做什么。 For instance, Network has such nice stuff as NetworkAvailabilityChanged event, and UploadFile method. 例如,网络具有诸如NetworkAvailabilityChanged事件和UploadFile方法之类的好东西。 On the other hand, NetworkInterface can give you a bunch of specific technical info such as speed or whether it supports multicast. 另一方面,NetworkInterface可以为您提供许多特定的技术信息,例如速度或是否支持多播。

BTW, there is nothing undocumented about using a class from Microsoft.VisualBasic namespace - it's the core idea behind .NET that you can use classes from assemblies regardless of the language they were written in. 顺便说一句,使用Microsoft.VisualBasic命名空间中的类并没有任何文献记载-这是.NET的核心思想,您可以使用程序集中的类而不管它们是以什么语言编写的。

What I generally do is write a small app, then load then project in Reflector and disassemble it. 我通常要做的是编写一个小应用程序,然后加载然后在Reflector中投影并反汇编它。

but you can use this class: System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged 但您可以使用此类:System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged

This appears to work. 这似乎起作用。 It's probably very undocumented usage though: 不过,这可能是非常未记录的用法:

        Microsoft.VisualBasic.Devices.Network net = new Microsoft.VisualBasic.Devices.Network();
        if (net.IsAvailable)
        {
            Text = "Network is available";
        }
        else
        {
            Text = "Network unavailable";
        }

Note that I needed to add a reference to Microsoft.VisualBasic to my project. 请注意,我需要在项目中添加对Microsoft.VisualBasic的引用。

难道不是整个VB库中的“我的”东西吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM