进程中如何通过Process.Start()打开程序
做一个开启远程机器客户程序的程序,用Socket做,打开程序用Process.Start()做,调试模式下可以打开客户程序,正常打开exe文件时,就打不开客户程序。
private void Form1_Load(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(NewMethod));
th.IsBackground = true;
th.Start();
}
private void NewMethod()
{
while (true)
{
try
{
int port = 2000;
string host = "192.168.0.60";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(ipe);
s.Listen(20);
Socket temp = s.Accept();
Console.WriteLine("get a connect");
string revStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = temp.Receive(recvBytes);
revStr += Encoding.ASCII.GetString(recvBytes).Replace("\0","");
if (revStr == "shutdown")
{
System.Diagnostics.Process.Start("cmd.exe", "shutdown -s -t 0");
}
if (revStr == "osk")
{
System.Diagnostics.Process.Start("osk.exe");
}
if (revStr == "notepad")
{
System.Diagnostics.Process.Start("osk.exe");
}
listBox1.Items.Add(DateTime.Now.ToString()+ " "+ revStr);
string sendStr = "OK,Client Send Message Successful!";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
temp.Send(bs, bs.Length, 0);
temp.Close();
s.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}