C# 中怎么判断本地连接是否连接,如果没连接怎么实现连接??

解决方案 »

  1.   

    如果是说的网线有没有插好:C# 中怎么判断本地连接是否连接:NetworkInterface.GetIsNetworkAvailable()如果没连接怎么实现连接:弹出个提示框,提示用户检查网线或网络
      

  2.   

    public static bool Ping(string remoteHost)
            {
                bool Flag = false;
                Process proc = new Process();
                try
                {
                    proc.StartInfo.FileName = "cmd.exe";
                    proc.StartInfo.UseShellExecute = false;
                    proc.StartInfo.RedirectStandardInput = true;
                    proc.StartInfo.RedirectStandardOutput = true;
                    proc.StartInfo.RedirectStandardError = true;
                    proc.StartInfo.CreateNoWindow = true;
                    proc.Start();
                    string dosLine = @"ping -n 1 " + remoteHost;
                    proc.StandardInput.WriteLine(dosLine);
                    proc.StandardInput.WriteLine("exit");
                    while (proc.HasExited == false)
                    {
                        proc.WaitForExit(500);
                    }
                    string pingResult = proc.StandardOutput.ReadToEnd();
                    if (pingResult.IndexOf("(0% loss)") != -1)
                    {
                        Flag = true;
                    }
                    proc.StandardOutput.Close();
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    try
                    {
                        proc.Close();
                        proc.Dispose();
                    }
                    catch
                    {
                    }
                }
                return Flag;
            }remoteHost填写你要ping的服务器
      

  3.   

    刚才测试一下,可以用:
    首先添加Microsoft Shell Control And Automation引用:        static void Main(string[] args)
            {
                NetWork("本地连接", "启用");//输入你的本地连接名称和要做的操作:启用或停用
            }        static void NetWork(string netWorkName, string operation)
            {
                Shell32.Shell shell = new Shell32.ShellClass();
                Shell32.Folder folder = shell.NameSpace(49);
                foreach (Shell32.FolderItem fi in folder.Items())
                {
                    if (fi.Name != netWorkName)
                        continue;
                    Shell32.ShellFolderItem folderItem = (Shell32.ShellFolderItem)fi;
                    foreach (Shell32.FolderItemVerb fiv in folderItem.Verbs())
                    {
                        if (!fiv.Name.Contains(operation))
                            continue;
                        else
                        {
                            fiv.DoIt();
                            Thread.Sleep(1000);
                            break;
                        }
                    }
                }
            }
      

  4.   

    如果你不知道怎么添加Microsoft Shell Control And Automation引用,请参考一下步骤:
    在你的解决方案资源管理器的bin上右键->选择添加引用->选择->COM标签->找到这个引用按确定即可