本地连接 C# 中怎么判断本地连接是否连接,如果没连接怎么实现连接?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果是说的网线有没有插好:C# 中怎么判断本地连接是否连接:NetworkInterface.GetIsNetworkAvailable()如果没连接怎么实现连接:弹出个提示框,提示用户检查网线或网络 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的服务器 刚才测试一下,可以用:首先添加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; } } } } 如果你不知道怎么添加Microsoft Shell Control And Automation引用,请参考一下步骤:在你的解决方案资源管理器的bin上右键->选择添加引用->选择->COM标签->找到这个引用按确定即可 Excel导出问题(导出时不去掉前面的0) “并非所有的代码路径都返回值”大家来帮帮忙啦~~ C# SMTP求助~~ 求个.NET C/S 传文件到数据库的方法 winform中的combobox控件 C# tabcontorl VS2010团队资源管理器报错解决呢? 真的没分了,只想请教大家 SQLSEVER2000 创建的.bak文件和在企业管理器中选择某数据库备份的无后缀文件有什么区别? 请问各位大哥:你们用什么工具做Web报表? 有关.net组件与Com+组件的区别的问题 控制菜单 .txt和.jpg等格式附件不能打开
{
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的服务器
首先添加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;
}
}
}
}
在你的解决方案资源管理器的bin上右键->选择添加引用->选择->COM标签->找到这个引用按确定即可