Windows 每5分钟写入驱动器信息的窗口服务
我想编写每5分钟执行一次的窗口服务,它将在数据库中写入driverinfo。我写了这个程序。我的逻辑正确吗?请任何人谁可以审查它。这行不行Windows 每5分钟写入驱动器信息的窗口服务,windows,service,Windows,Service,我想编写每5分钟执行一次的窗口服务,它将在数据库中写入driverinfo。我写了这个程序。我的逻辑正确吗?请任何人谁可以审查它。这行不行 [RunInstaller(true)] public class WinServiceInstaller : Installer { public WinServiceInstaller() { var processInstaller = new ServiceProcessInstaller(); var
[RunInstaller(true)]
public class WinServiceInstaller : Installer
{
public WinServiceInstaller()
{
var processInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();
//must be the same as what was set in Program’s constructor
serviceInstaller.ServiceName = "service1";
//set the privileges
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.DisplayName = "My Display Name";
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.Description = "Description of your service";
this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
}
}
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
GetDriveInfo();
}
protected override void OnStop()
{
}
static void GetDriveInfo()
{
DriveInfo[]alldrives = DriveInfo.GetDrives();
if (alldrives.IsReadOnly)
{
StreamWriter writer = new StreamWriter("d:\\drives.text");
writer.WriteLine("all drives are readyonly");
}
else
{
StreamWriter writer = new StreamWriter("d:\\driveInfo.text");
writer.WriteLine("all drives are not ready only");
writer.Flush();
writer.Close();
foreach(DriveInfo info in alldrives )
{
if(info.IsReady)
{
try
{
string name = info.Name;
string drivetype = info.DriveType.ToString();
string remainingSpace = info.TotalFreeSpace.ToString();
string totalsize = info.TotalSize.ToString();
string lastaccesstime = info.RootDirectory.LastAccessTime.ToString();
string connectionstring = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connectionstring);
con.Open();
SqlCommand cmnd = new SqlCommand("[dbo].[SP_INSERT_DRIVE_INFO]", con);
cmnd.CommandType = CommandType.StoredProcedure;
cmnd.Parameters.AddWithValue("@Driver_Name", name);
cmnd.Parameters.AddWithValue("Driver_type", drivetype);
cmnd.Parameters.AddWithValue("Remaining_Space", remainingSpace);
cmnd.Parameters.AddWithValue("Total_size", totalsize);
cmnd.Parameters.AddWithValue("LastAcess_time", lastaccesstime);
cmnd.ExecuteNonQuery();
con.Close();
}
catch (SqlException ex)
{
}
}
else
{
writer.WriteLine("drives are not ready");
writer.Close();
}
}
}
}
static void timer()
{
System.Threading.Timer timer = new System.Threading.Timer(OnElapsedTime,null,0,18000);
timer.Change(Timeout.Infinite, Timeout.Infinite);
}
private static void OnElapsedTime(object state)
{
GetDriveInfo();
}
}
}
我认为这将更适合这个问题,因为这个问题似乎是离题的,因为它不是在问一个问题。您可能可以获得有关代码审阅的帮助。基本上,我希望windows服务在5分钟后执行该函数。我正在调用private static void OneReleasedTime(对象状态){GetDriveInfo();}