我编写了一个小实用程序,它使用MSRA.exe创建远程帮助邀请文件,并立即开始侦听帮助台环境的帮助请求。该应用程序运行良好,线程MSRA.exe对用户完全不可操作,但我遇到的一个问题是,如果用户通过终止进程或somthing不正确地终止连接,那么下次我的应用程序运行和MSRA。 exe被称为发送一个对话框,说明用户没有正确关闭最后一个连接并且桌面设置没有恢复,他们是否希望立即恢复桌面设置?好吧,因为我隐藏了MSRA,对话框也被隐藏,并在等待响应时暂停MSRA程序,有没有办法检查这个对话框并强制回答?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using System.Management;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
public Form1()
{
InitializeComponent();
}
public void EndSession()
{
if (button1.Text != "Launch Connection")
{
button1.Text = "Closing Connection...";
this.Refresh();
if (p.HasExited == false)
{
KillProcessAndChildren(p.Id);
}
if (File.Exists(System.IO.Path.GetTempPath() + "Invitation.msrcincident") == true)
{
File.Delete(System.IO.Path.GetTempPath() + "Invitation.msrcincident");
}
}
Application.Exit();
}
private void KillProcessAndChildren(int pid)
{
using (var searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid))
using (ManagementObjectCollection moc = searcher.Get())
{
foreach (ManagementObject mo in moc)
{
KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
}
try
{
Process proc = Process.GetProcessById(pid);
proc.CloseMainWindow();
if (!proc.WaitForExit((int)TimeSpan.FromSeconds(3).TotalMilliseconds))
{
proc.Kill();
}
}
catch (ArgumentException)
{ /* process already exited */ }
}
}
public void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Launch Connection")
{
button1.Text = "Opening Connection...";
this.Refresh();
if (File.Exists(System.IO.Path.GetTempPath() + "Invitation.msrcincident") == true)
{
File.Delete(System.IO.Path.GetTempPath() + "Invitation.msrcincident");
}
string fileurl = System.IO.Path.GetTempPath() + "Invitation.msrcincident";
p.StartInfo.UseShellExecute = true;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.FileName = "Msra.exe";
p.StartInfo.Arguments = "/saveasfile " + fileurl + " MyPass";
Console.WriteLine(p.StartInfo.Arguments);
p.Start();
while (File.Exists(System.IO.Path.GetTempPath() + "Invitation.msrcincident") == false)
{
Thread.Sleep(1000);
}
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://ftp.MyFTP.net/" + System.Environment.UserName + "." + Dns.GetHostName() + ".msrcincident");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("MyUser", "MyPass");
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
Stream ftpstream = request.GetRequestStream();
FileStream stream = File.OpenRead(fileurl);
int length = 1024;
byte[] buffer = new byte[length];
int bytesRead = 0;
do
{
bytesRead = stream.Read(buffer, 0, length);
ftpstream.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
stream.Close();
ftpstream.Close();
button1.Text = "Connection Open";
}
else
{
EndSession();
}
}
private void button2_Click(object sender, EventArgs e)
{
EndSession();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = System.Environment.UserName;
textBox2.Text = Dns.GetHostName();
Process[] processes = Process.GetProcessesByName("msra");
foreach (Process process in processes)
{
process.Kill();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
EndSession();
}
}
}