SqlConnection con = new SqlConnection("XXX&qu
为了解决用户可能碰到关于"急!C#或者CMD获取本机网络连接的名?急!!C#或者CMD获取"相关的问题,酷网经过收集整理为用户提供相关的解决办法,请注意,解决办法仅供参考,不代表本网同意其意见,如有任何问题请与本网联系。"急!C#或者CMD获取本机网络连接的名?急!!C#或者CMD获取"相关的详细问题如下:急!! C#或者CMD获取本机网络连接的名称
思路:同时执行时,需要顺序执行,如果需要同时输出结果,需要让cmd异步执行,同时将结果通过代理方式
扩展阅读,根据您访问的内容系统为您准备了以下扩展内容,希望对您有帮助。
急!! C#或者CMD获取本机网络连接的名称
cmd
ipconfig/all追问ipconfig 获取的不全
我要这个列表
LAN或高速Internet
无线网络连接 本地连接
拨号
中国电信 中国移动CMWAP
宽带
宽带连接
C#运行.cmd文件
一楼和二楼都是正解:
1. 一楼的System.Diagnostics.Process.Start("E:\text.cmd"); 代码意思就等于你用鼠标双击“text.cmd”这个文件;
这样简单易了,
2.二楼的写的就比较清楚拉,他是在程序中把“开始--运行---再输入cmd---再输入你的命令”这个命令执行一遍
3.我这边有个例子,就是查找局域网中所有IP地址的代码,愿意看下的话,我放下面拉,(我的form中放拉一个button和一个listBox控件,在下面标记的地方“就可以输入你自己的命令”)
private void button1_Click(object sender, EventArgs e)
{
ArrayList l= GetAllLocalMachines();
listBox1.Items.Clear();
for (int i = 0; i < l.Count; i++)
{
listBox1.Items.Add(l[i]);
}
listBox1.Dock = DockStyle.Bottom;
this.Controls.Add(listBox1);
}
static ArrayList GetAllLocalMachines()
{
Process p = new Process();
//设定程序名
p.StartInfo.FileName = "cmd.exe";
//关闭Shell的使用
p.StartInfo.UseShellExecute = false;
//重定向标准输入
p.StartInfo.RedirectStandardInput = true;
//重定向标准输出
p.StartInfo.RedirectStandardOutput = true;
//重定向错误输出
p.StartInfo.RedirectStandardError = true;
//设置不显示窗口
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("arp -a");//这里就可以输入你自己的命令
p.StandardInput.WriteLine("exit");
ArrayList list = new ArrayList();
StreamReader reader = p.StandardOutput;
string IPHead = Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString().Substring(0, 3);
for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
line = line.Trim();
if (line.StartsWith(IPHead) && (line.IndexOf("dynamic") != -1))
{
string IP = line.Substring(0, 15).Trim();
string Mac = line.Substring(line.IndexOf("-") - 2, 0x11).Trim();
LocalMachine localMachine = new LocalMachine();
localMachine.MachineIP = IP;
localMachine.MachineMAC = Mac;
localMachine.MachineName = "";
list.Add(IP);
}
}
return list;
}
效果如下: 不知道这样对你有没有帮助~~
C#如何获取CMD命令行输出的部分字符
其实可以直接用java.exe 命令,不用cmd.exe ,如下代码:
using System;using System.ComponentModel;using System.Diagnostics; namespace ProcessDemo{ class Program { static void Main(string[] args) { try { Process p = new Process(); p.StartInfo.FileName = "java.exe"; p.StartInfo.Arguments = "-version"; p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.Start(); p.WaitForExit(); String result = p.StandardOutput.ReadToEnd(); if (String.IsNullOrEmpty(result)) { Console.WriteLine("StandardOutput是空的。"); } result = p.StandardError.ReadToEnd(); Console.WriteLine(result); } catch(Win32Exception e){ Console.WriteLine(e.Message); } Console.WriteLine("Program end"); Console.ReadKey(); } }}
运行结果: