asked on
...
using System.IO;
using GetProcessStats.AutoWriteToFile;
class Program
{
static void Main(string[] args)
{
FileWriter FW = new FileWriter();
ProcessInfo WscriptInfo = new ProcessInfo();
WscriptInfo.GetProcess("D011", "mspaint");
FW.?????? ("", "");
}
}
using System;
using System.IO;
using System.Diagnostics;
using GetProcessStats.AutoWriteToFile;
namespace GetProcessStats
{
class ProcessInfo
{
PerformanceCounter pc = new PerformanceCounter();
public Process GetProcess(string hostname, string processName)
{
Process[] ps = Process.GetProcessesByName(processName, hostname);
if (ps.Length == 0)
return null;
else
return ps[0];
}
public bool IsProcessAlive(string hostname, string processName)
{
Process p = GetProcess(hostname, processName);
if (p == null)
{
return false;
}
else
{
return !p.HasExited;
}
}
public string GetProcessCPU(string hostname, string processName)
{
string str = "";
pc.CategoryName = "Process";
pc.CounterName = "% Processor Time";
pc.InstanceName = processName;
pc.MachineName = hostname;
try
{
str = pc.NextValue().ToString();
}
catch
{
str = "Error";
}
return str;
}
public string GetProcessRAM(string hostname, string processName)
{
Process p = GetProcess(hostname, processName);
if (p == null)
{
return "Error";
}
else
{
return p.PagedMemorySize64.ToString();
}
}
}
class Program
{
static void Main(string[] args)
{
FileWriter FW = new FileWriter();
ProcessInfo WscriptInfo = new ProcessInfo();
WscriptInfo.GetProcess("D011", "mspaint");
FW. ("", "");
}
}
}
----------------------------------------------------------------------
Separate code file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace GetProcessStats
{
namespace AutoWriteToFile
{
public static class FileWriter
{
// Set this to false to stop writing to file
public static bool _WriteToFile = true;
void WriteToFile(string FileDest, string StrOutput)
{
TextWriter tw;
if (_WriteToFile)
{
try
{
// create a writer and open the file
tw = new StreamWriter(FileDest);
// write a line of text to the file
tw.WriteLine(StrOutput);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// close the stream
tw.Close();
}
}
}
}
}
}