0

VS 2008

I have this code snippet I found on a VB website.

But for some reason I am having trouble converting it to C#.

My.Computer.Network.IsAvailable

Many thanks,

1
  • What's your compilation error? Commented Jul 2, 2009 at 3:12

5 Answers 5

4
using System.Net.NetworkInformation;

    internal class Program
    {
        private static void Main()
        {
            NetworkInterface.GetIsNetworkAvailable();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

3

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.

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. For instance, Network has such nice stuff as NetworkAvailabilityChanged event, and UploadFile method. On the other hand, NetworkInterface can give you a bunch of specific technical info such as speed or whether it supports multicast.

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.

1 Comment

The only question I had about doing what I did was creating my own instance of the network class - the My.etc creates the class for you - perhaps in a special way I don't know about in some circumstances.
2

What I generally do is write a small app, then load then project in Reflector and disassemble it.

but you can use this class: System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged

Comments

1

Isn't the whole "My" thing from a VB library?

1 Comment

The "My" namespace is from a library written by the Visual Basic Team, but it forms a part of the standard .NET install and is perfectly usable by (in fact, very useful to) C# development, or indeed any .NET language at all.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.