How to create service for getting details of drive

62 Views Asked by At

I need to create service for monitoring system in that I have to get details of system drive (like whose system, which username, drive name, drive space, total drive space) means network admin should get to know the how much drive are there, or space are available by their hostname and username.  this window service will call web Api every 5 min, service will collect the details of drive and send to web Api after that web Api store the detail in database after that portal will collect the data from database and details will show in portal. and for this need to use only dapper in portal and database. Asp.net core use in web Api and in window service need to use rest Line (Rest Sharp) for calling Api.

Could anyone please guide with some step or code?

I had tried this:

Windows Service:

using System;
using System.IO;
using RestSharp;

namespace DriveMonitorService
{
    class Program
    {
        static void Main(string[] args)
        {
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                if (drive.IsReady)
                {
                    var client = new RestClient("http://your-api-url/api/drives");
                    var request = new RestRequest(Method.POST);
                    request.AddJsonBody(new
                    {
                        SystemName = Environment.MachineName,
                        UserName = Environment.UserName,
                        DriveName = drive.Name,
                        DriveSpace = drive.TotalFreeSpace,
                        TotalDriveSpace = drive.TotalSize
                    });
                    IRestResponse response = client.Execute(request);
                }
            }
        }
    }
}
1

There are 1 best solutions below

0
Greg Valainis On

If you are using the latest versions of dotnet, creating a service is easy enough to do. There is a worker service template that will get you started. dotnet new worker --name DriveMonitorService will create a worker project that seems to be exactly what you need. From there, you will need to edit the Worker.cs file that the template created. For your scenario of polling every five minutes something like this should do the trick:

using System;
using System.IO;
using RestSharp;

namespace DriveMonitorService;

public class Worker : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                if (drive.IsReady)
                {
                    var client = new RestClient("http://your-api-url/api/drives");
                    var request = new RestRequest(Method.POST);
                    request.AddJsonBody(new
                    {
                        SystemName = Environment.MachineName,
                        UserName = Environment.UserName,
                        DriveName = drive.Name,
                        DriveSpace = drive.TotalFreeSpace,
                        TotalDriveSpace = drive.TotalSize
                    });
                    IRestResponse response = client.Execute(request);
                }
            }
            await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
        }
    }
}

Documentation for dotnet worker services can be found here:

https://learn.microsoft.com/en-us/dotnet/core/extensions/workers