首页 > 系统相关 >c# 进程判断

c# 进程判断

时间:2024-06-15 17:56:07浏览次数:16  
标签:判断 string Show c# System ToString 进程 path line

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace ProcessMonitoring
{
class ProcessMonitoring
{
public bool CheckForIllegalCrossThreadCalls { get; private set; }
string logPath = System.Environment.CurrentDirectory + @"\log.txt";
string date = System.DateTime.Now.ToString() + ":";
/// <summary>
/// 根据运行路径截取进程名
/// </summary>
/// <param name="pathName"></param>
/// <returns></returns>
public string getProcessName(string pathName)
{
string processName = "";
try
{
processName = pathName.Substring(pathName.LastIndexOf(@"\") + 1, pathName.IndexOf(".") - pathName.LastIndexOf(@"\") - 1);
}
catch (Exception e)
{
MessageBox.Show("获取文件"+e.Message.ToString());

}
return processName;
}

/// <summary>
///通过程序名查找对应进程,返回查找的结果或者程序状态
/// </summary>
/// <param name="exeName"></param>
/// <returns></returns>
public string findProcess(string exeName)
{
Thread.Sleep(1000);
string path="";//进程路径
Process[] process = Process.GetProcessesByName(exeName);
// MessageBox.Show(process.Length.ToString());
if (process.Length>0)
{
foreach (Process p in process)
{
try
{
path = p.MainModule.FileName.ToString();
// MessageBox.Show(path);
}
catch (Exception e)
{

MessageBox.Show("获取进程集合" + e.ToString());
}
}

}
if (process.Length == 0)
{
return System.DateTime.Now.ToString() + ":" + exeName + ".exe 未找到进程";
}
else if(process.Length >0)
{

Process p = process[0];

if (p.Responding)
{
//进程总线程数
int thSumCount = p.Threads.Count;
//线程无响应数量
int suspendCount = 0;

foreach (ProcessThread t in p.Threads)
{

//进程处于等待状态
if (t.ThreadState == System.Diagnostics.ThreadState.Wait)
{
//等待原因为挂起
if (t.WaitReason == ThreadWaitReason.Suspended)
{
suspendCount++;
}
}
//除了一个异常报错的对话框线程,其他线程都挂起了,说明进程已经卡死
if (thSumCount > 1 && thSumCount == suspendCount + 1)
{
return date + exeName + ".exe 程序卡死无响应,总线程:" + thSumCount + " " + "挂起线程:" + suspendCount;
}
else
{
// return System.DateTime.Now.ToString() + ":" + exeName + ".exe 程序正常运行";
}

}
}

}
return "";
}
//创建文件
public bool createFile(string filePath)
{
//创建文件
if (!File.Exists(filePath))
{
using (FileStream fileStream = File.Create(filePath)) {

};
}

return !File.Exists(filePath)?false:true;
}
/// <summary>
/// 读取文件,返回一个string类型的list集合
/// </summary>
/// <param name="filePath"></param>
/// <returns>list集合</returns>

public List<string> readFile(string filePath)
{
int cnt = 0;
List<string> list = new List<string>();
try
{
// 创建一个StreamReader对象来读取文件
using (StreamReader sr = new StreamReader(filePath))
{
string line;
// 逐行读取文件,直到读取完毕
while ((line = sr.ReadLine()) != null)
{
// 在这里处理每一行,例如打印到控制台
Console.WriteLine(line);
list.Add(line);
cnt++;
}
sr.Close();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
finally
{

}
return list;
}

//删除已添加的程序
public bool deleteFile(string path,string text)
{

bool flag = false;
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite,FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
//读取文件内容
List<string> lines = new List<string>();
string line;
while ((line = sr.ReadLine()) != null)
{
if (text != getProcessName(line))
{
lines.Add(line.ToString());
}
}

fs.SetLength(0);
// MessageBox.Show("lines"+lines.Count.ToString());
foreach (var tex in lines)
{

if (tex != "")
{
// MessageBox.Show("添加");
// insertFile(path, tex);
sw.WriteLine(tex.Trim());
}
}
lines.Clear();//清空集合
sw.Close();
sr.Close();
fs.Close();
if (readFile(path, text) != true)
{
flag = true;

}
return flag;
}
/// <summary>
/// 读取文件 根据count判断返回多少行
/// </summary>
/// <param name="filePath"></param>
/// <param name="count"></param>
/// <returns></returns>
public int readFile(string filePath,int count)
{
int cnt = 0;
try
{
// 创建一个StreamReader对象来读取文件
using (StreamReader sr = new StreamReader(filePath))
{
string line;
// 逐行读取文件,直到读取完毕
while ((line = sr.ReadLine()) != null)
{
// 在这里处理每一行,例如打印到控制台
Console.WriteLine(line);
cnt++;
if (cnt==count)
{
break;
}
}
sr.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{

}
return cnt;
}
/// <summary>
/// 判断文本文件中是否存在这行数据
/// </summary>
/// <param name="filePath"></param>
/// <param name="text"></param>
/// <returns></returns>
public bool readFile(string filePath, string text)
{
bool flag = false;
try
{
// 创建一个StreamReader对象来读取文件
using (StreamReader sr = new StreamReader(filePath))
{
string line;
// 逐行读取文件,直到读取完毕
while ((line = sr.ReadLine()) != null)
{
// 在这里处理每一行,例如打印到控制台
if (text == getProcessName(line))
{
flag = true;
break;
}
}
sr.Close();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
finally
{

}
return flag;
}
/// <summary>
/// 启动程序
/// </summary>
/// <param name="path">程序路径</param>
public void startProcess(string path)
{
Thread.Sleep(500);
//MessageBox.Show(logPath);
try
{
if (findProcess(path) == "")
{
MessageBox.Show(path+".exe 已存在!");
return;
}
else
{

File.WriteAllText(logPath, date+ File.ReadAllText(logPath).Insert(0, "开始启动进程"+path + ".exe\r\n"));
Process.Start(path);
if (findProcess(path) == "")
{

File.WriteAllText(logPath, date + File.ReadAllText(logPath).Insert(0, "启动进程" + path + ".exe成功" + "\r\n"));
}
else
{

}
}



}
catch (Exception e)
{

MessageBox.Show(e.Message.ToString());
}
}
/// <summary>
/// 杀死进程
/// </summary>
/// <param name="processName">进程名</param>
public void killProcess(string processName)
{
try
{
Process[] process = Process.GetProcesses();
if (findProcess(processName) == "")
{
foreach (Process item in process)
{
if (item.ProcessName == processName)
{
item.Kill();
File.WriteAllText(logPath, File.ReadAllText(logPath).Insert(0, processName+".exe 进程已结束" + "\r\n")) ;
}
}
}
else
{

File.WriteAllText(logPath, date+File.ReadAllText(logPath).Insert(0, "进程未启动" + "\r\n"));
}


}
catch (Exception e)
{

MessageBox.Show("结束进程:"+e.Message.ToString());
}
}
}
}

 

标签:判断,string,Show,c#,System,ToString,进程,path,line
From: https://www.cnblogs.com/l-xs/p/18249549

相关文章

  • c# form主页
    usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Diagnostics;namespaceProcessMonitoring{publicpartialclassFor......
  • c# 文件读取
    usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Threading;namespaceProcessMonitoring{publicpartialclassForm2......
  • docker没外网安装方法
    docker没外网安装方法需要先清理之前docker的依赖,避免影响安装失败1,下载docker的压缩tar包百度云Docker18.06.1地址:https://pan.baidu.com/s/1YdN9z72QutPkHBfLq06H1A密码:dvvh2,解压tar包#解压tar-xvfdocker-18.06.1-ce.tgz#将压缩的全部文件复制到/usr/bincpdoc......
  • c# excel
    usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.IO;usingSystem.Web;namespaceGetPCInformation{classExcel{publicvoidExportExcel(DataTabledt){//设置导出文件路径stringpath=HttpC......
  • Oracle 性能调优 10053事件
    思维导图10053事件概述我们在查看一条SQL语句的执行计划时,只看到了CBO最终告诉我们的执行计划结果,但是我们并不知道CBO为何要这样做。特别是当执行计划明显失真时,我们特别想搞清楚为什么CBO会做出这样的一个选择,那么就可以用10053事件来分析SQL分析过程的trace文件。同100......
  • c# 系统信息
    usingSystem;usingSystem.Windows.Forms;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Management;usingSystem.IO;usingSystem.Diagnostics;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Runtime.InteropServices;namespaceGetPCInfo......
  • 一篇文章教你如何解决vs编译器中,库函数scanf的正常使用!!!
    前言:这将是我写的第二篇博文了,以后我将会坚持每天花固定时间来写博客,希望我当我写了一定数量的文章之后,我自己简单的就能够写出质量优秀文章来,加油!!!一、scanf函数无法正常使用的原因1.错误实例在vs中如果想直接实现并使用scanf函数,很抱歉直接使用,用不了,因为编译器压根就......
  • 生产环境使用10053分析Oracle的执行计划
    【问题现象】在SQL出现性能问题后,通过分析统计信息、直方图、SQLAWR、查看执行计划等,仍然找不出为什么SQL要选择差的执行计划,就需要通过10053查看这个SQL的执行计划的更详细的信息。【操作方法】通过10053事件来查看执行计划和详细的SQL解析过程,10053的trace文件提供了Oracle......
  • 算法训练(leetcode)第九天 | 232. 用栈实现队列、225. 用队列实现栈、20. 有效的括号、1
    刷题记录232.用栈实现队列225.用队列实现栈20.有效的括号1047.删除字符串中的所有相邻重复项232.用栈实现队列leetcode题目地址考察栈与队列之间的特性。栈:后进先出(先进后出)——FILO。队列:先进先出——FIFO。所以使用两个栈模拟队列,分别为in和out。当入队新......
  • Docker 入门
    Docker是一个开源的应用容器引擎,它允许开发者打包他们的应用以及应用的运行环境到一个可移植的容器中。以下是学习Docker的推荐路线:###入门阶段1.**了解容器化的概念**:  -容器化与虚拟化的区别  -容器化的优势2.**Docker简介**:  -Docker的历史和目标......