我正在使用 C# 程序来自动重启我的 DSL 路由器(如果它关闭)。到目前为止我写了这个:
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Net;
namespace RebootRouter
{
static class Program
{
[STAThread]
static void Main()
{
bool ProgramLoop = true;
do
{
CheckAndRun();
System.Threading.Thread.Sleep(180000);
}
while (ProgramLoop == true);
}
public static void CheckAndRun()
{
if (ScreenSaverCheck.IsScreenSaverRunning() == true)
{
if (InternetCheck.IsConnected() == false)
{
string FirefoxBinary = (string)Registry
.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\
Microsoft\Windows\CurrentVersion\
App Paths\firefox.exe", null, false);
Process LaunchMacro = new Process();
LaunchMacro.StartInfo.FileName = FirefoxBinary;
LaunchMacro.StartInfo.Arguments = "imacros://run/?
m=RebootRouter.iim";
LaunchMacro.Start();
}
}
}
public class ScreenSaverCheck
{
public static bool IsScreenSaverRunning()
{
Process[] FullProcessList = Process.GetProcesses();
foreach (Process ProcessFromList in FullProcessList)
{
if (ProcessFromList.ProcessName.EndsWith(".scr"))
{
return true;
}
}
return false;
}
}
public class InternetCheck
{
public static bool IsConnected()
{
try
{
IPAddress[] GoogleIPAddressList = Dns.
GetHostAddresses("google.com");
if (GoogleIPAddressList[0].ToString().Length > 6)
{
return true;
}
}
catch (Exception)
{
}
return false;
}
}
}
}
它按预期工作,但问题是该进程每次循环时都会将其内存使用量增加 15 KB。这是正常的还是有办法优化?任何帮助将不胜感激。