110,539
社区成员
发帖
与我相关
我的任务
分享
[DllImport("wininet.dll", EntryPoint = "InternetGetConnectedState")]
public extern static bool InternetGetConnectedState(out int conState, int reder);
[DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
static List<string> list = new List<string>();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
public static extern int GetWindowText(int hWnd, IntPtr lpString, int nMaxCount);
public delegate bool CallBack(int hwnd, int lParam);
public delegate bool EnumWindowsProc(int hWnd, int lParam);
[DllImport("user32.dll")]
public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern int EnumThreadWindows(int dwThreadId, CallBack lpfn, int lParam);
[DllImport("user32.dll")]
public static extern int EnumWindows(CallBack lpfn, int lParam);
[DllImport("user32.dll")]
public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);
static void Main(string[] args)
{
try {
IntPtr hwnd = FindWindow(null, "测试系统");
int cc = hwnd.GetHashCode();
string error="通讯链接失败";
string lujing = @"C:\Program Files\测试系统\fwtel.exe";
EnumChildWindows(hwnd, EnumWindowsMethod, IntPtr.Zero);
foreach(var l in list ){
if (l.IndexOf(error) != -1 && l.IndexOf("运行时错误")!=-1)
{
StreamWriter sw = File.AppendText("E:\\ErrorLog\\Log.txt");
string w = "通讯链接失败 错误时间:" + DateTime.Now+"\n";
sw.WriteLine(w);
sw.Close();
Process[] processes;
processes = Process.GetProcesses();
string str = "";
foreach (Process p in processes)
{
try
{
str = p.MainModule.FileName;
if (str.Equals(lujing,StringComparison.CurrentCultureIgnoreCase))
{
p.CloseMainWindow();
p.Kill();
StreamWriter swww = File.AppendText("E:\\ErrorLog\\Log.txt");
string www = "程序关闭 操作时间:" + DateTime.Now + "";
swww.WriteLine(www);
swww.Close();
Process.Start(lujing);
StreamWriter swwww = File.AppendText("E:\\ErrorLog\\Log.txt");
string wwww = "程序重新启动 操作时间:" + DateTime.Now + "";
swwww.WriteLine(wwww);
swwww.Close();
}
}
catch (Exception ex)
{
StreamWriter sww = File.AppendText("E:\\ErrorLog\\Log.txt");
string ww = "程序错误 错误问题"+ex.ToString()+"错误时间:" + DateTime.Now + "";
sww.WriteLine(ww);
sww.Close();
}
}
}
}
}
catch (Exception ex)
{
StreamWriter sww = File.AppendText("E:\\ErrorLog\\Log.txt");
string ww = "程序错误 错误问题" + ex.ToString() + "错误时间:" + DateTime.Now + "\n";
sww.WriteLine(ww);
sww.Close();
}
}
private static bool EnumWindowsMethod(int hWnd, int lParam)
{
IntPtr lpString = Marshal.AllocHGlobal(200);
GetWindowText(hWnd, lpString, 200);
var text = Marshal.PtrToStringAnsi(lpString);
if (!string.IsNullOrWhiteSpace(text))
list.Add(text);
return true;
}