例如游戏战场3.文件名是bf3.exe 我想我的应用程序将在我第一次运行游戏时检测到,然后当我退出游戏时。
我在Form1中做过:
private ProcessStartInfo bf3;
在构造函数中:
bf3 = new ProcessStartInfo("bf3.exe");
我不确定使用ProcessStartInfo是否好主意以及下一步该做什么。
编辑:
这是我现在的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using DannyGeneral;
namespace CpuUsage
{
public partial class Form1 : Form
{
private DateTime dt;
private DateTime dt1;
private PerformanceCounter theCPUCounter;
private PerformanceCounter theMemCounter;
private PerformanceCounter specProcessCPUCounter;
private float cpuUsage;
private float memUsage;
private List<float> Values;
public Form1()
{
InitializeComponent();
isProcessRunning();
dt = DateTime.Now;
Values = new List<float>();
theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
theMemCounter = new PerformanceCounter("Memory", "Available MBytes");
specProcessCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
memUsage = theMemCounter.NextValue();
label1.Text = memUsage.ToString();
Logger.Write("Memory Usage " + memUsage.ToString());
cpuUsage = this.theCPUCounter.NextValue();
label2.Text = cpuUsage.ToString();
Logger.Write("Cpu Usage " + this.cpuUsage.ToString());
Values.Add(cpuUsage);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
dt1 = DateTime.Now;
float Maximum = Values.Max();
float Minimum = Values.Min();
float Average = Values.Average();
string t = string.Format("Maximum --- {0} , Minimum --- {1} , Average --- {2}", Maximum, Minimum, Average);
Logger.Write(t);
TimeSpan ts = (dt1 - dt);
string time = ts.ToString(@"hh\:mm\:ss");
Logger.Write("Time The Program Was Running --- " + time);
}
private void isProcessRunning()
{
while (true)
{
Process[] proclist = Process.GetProcessesByName("bf3.exe");
if (proclist.Length > 0)
{
Logger.Write("Battlefield 3 Started");
}
else
{
Logger.Write("Battlefield 3 Exited");
}
}
}
}
}
现在的问题是它进入thw while循环并且永远不会将它停留在循环中。 我也需要使用定时器代码,同时检查/等待bf3.exe是否启动或结束。
答案 0 :(得分:1)
你想查看天气bf3.exe正在运行或退出或是什么? 然后你应该尝试下面一段代码。
Process[] _process = null;
_process = Process.GetProcessesByName("bf3");
foreach (Process proc in _process)
{
proc.Kill();
MessageBox.Show(proc.ToString());
}
答案 1 :(得分:1)
在任何情况下,您都想检测正在运行的.exe文件吗?在c#中,也许这个链接也可以帮助你。
Detecting a Process is already running in windows using C# .net
答案 2 :(得分:0)
答案 3 :(得分:0)
您可以使用以下命令按名称获取正在运行的进程:
Process[] processes = Process.GetProcessesByName("process name");
您可以定期检查,或使用上一个答案中提到的挂钩。如果在BF3之后启动应用程序,则必须在启动期间获取一次进程。
答案 4 :(得分:0)
答案 5 :(得分:0)
您可以通过以下方式启动它:
System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Origin Games\Battlefield 3\bf3.exe"); //modify the start path for wherever your copy is located at
虽然我很确定运行该exe只启动Origin,然后启动Web客户端/服务器选择器,从而启动游戏。
在启动它的方法中,之后您可以立即运行一个无限循环,检查“bf3.exe”进程是否正在运行,如下所示:
while (true) // Run in a separate thread to prevent blocking if that will be a problem
{
Process[] proclist = Process.GetProcessByName("bf3.exe");
if (proclist.Length > 0)
{
//BF3 is running, do something here
}
else
{
//BF3 isn't running any more, do something here
}
}
答案 6 :(得分:0)
如果你想测量EXE运行时间,你可以使用秒表并在运行exe之前启动它,并在EXE停止时停止它。
粗略的代码是:
Stopwatch run_time = new Stopwatch();
run_time.Start();
Process.Start("bf3.exe"); //You will need to enter full path here
while(true)
{
Process[] pListofProcess = Process.GetProcesses();
bool bRunning = false;
foreach (Process p in pListofProcess)
{
string ProcessName = p.ProcessName;
ProcessName = ProcessName.ToLower();
if (ProcessName.CompareTo("bf3.exe") == 0)
{
bRunning = true;
}
}
if (bRunning == false)
{
run_time.Stop();
//Show elapsed time here.
break;
}
Thread.Sleep(1000);
}
答案 7 :(得分:0)
您需要通过名称获取所有流程,然后检查长度。
Process[] proc = Process.GetProcessesByName("process_name");
if (proc.Length > 0) Console.Write("Process is Running");
else Console.Write("Not running");