常用命令
net use \\192.168.1.2 "" /user:"Administrator"
private string CmdPing(string strIp)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string pingrst;
p.Start();
p.StandardInput.WriteLine("ping -n 1 " + strIp);
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
if (strRst.IndexOf("(0% loss)") != -1)
pingrst = "连接";
else if (strRst.IndexOf("Destination host unreachable.") != -1)
pingrst = "无法到达目的主机";
else if (strRst.IndexOf("Request timed out.") != -1)
pingrst = "超时";
else if (strRst.IndexOf("Unknown host") != -1)
pingrst = "无法解析主机";
else
pingrst = strRst;
p.Close();
return pingrst;
}
tcp 发送
public static bool NetworkCommand(string CommandText, string Ip, string Port)
{
try
{
byte[] bt = System.Text.Encoding.Default.GetBytes(CommandText);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(Ip), Convert.ToInt32(Port));
TcpClient tc = new TcpClient();
tc.Connect(ep);
NetworkStream nstream = tc.GetStream();
nstream.Write(bt, 0, bt.Length);
tc.Close();
return true;
}
catch (Exception ex)
{
throw ex;
}
return false;
}
tcp监听
private void SocketLis()
{
Int32 port = Config.SocketPort;
IPAddress localAddr = IPAddress.Any;//(/*"117.25.162.138"/*"127.0.0.1"*/);
// IPAddress localAddr = IPAddress.Parse("127.0.0.1");
tcl = new TcpListener(localAddr, port);
tcl.Start();
DrawListView("在" + port.ToString() + "端口启动监听");
while (true)
{
TcpClient tc = tcl.AcceptTcpClient();
DrawListView("收到网络上的信息");
NetworkStream netstream = tc.GetStream();
byte[] bt = new byte[256];
int i;
string revdate="";
DateTime dt;
while ((i = netstream.Read(bt, 0, bt.Length)) != 0)
{
revdate = System.Text.Encoding.Default.GetString(bt, 0, i);
}
DrawListView(revdate);
try
{
dt = DateTime.Parse(revdate);
TimeSpan ts = dt - DateTime.Now;
if (ts.Ticks > 0)
{
tr.Change(ts, new TimeSpan(-1));
}
}
catch(Exception ex)
{
DrawListView("Socket接收出错:"+ex.Message);
}
}
}