0

I'm creating a little stupid service as a joke, is supposed to create an "invincible" txt on the desktop which gets recreated if deleted. It works when debugged but when I install the service it doesn't create the txt.

I have :

  • execute the service with admin rights adding this line of code: <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" /> to the app.manifest).
  • I checked the "the authorize to interact whit desktop" checkmark on the service proprieties.

the code is working when I debug (i use Topshelf) but when I install the service it does not work.

the code which creates the txt(and the constructor):

 public Invincible()
    {
        _timer = new Timer(3000) { AutoReset = true };
        _timer.Elapsed += TimerElapsed;
    }

    private void TimerElapsed(object sender, ElapsedEventArgs e)
    {
        string[] frase = new string[] {"NON PUOI RIMUOVERE QUESTA MALEDIZIONE <3"};
        string curFile = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/Invincible_Curse.txt";
        string curFile2 = "C:/temp/Demos/Invincible_Curse.txt";
        if (!File.Exists(curFile))
        {
            File.AppendAllLines(curFile, frase);
        }
    }

the Main:

 static void Main(string[] args)
    {
        var exitCode = HostFactory.Run(x =>
        {
            x.Service<Invincible>(s =>
            {

                s.ConstructUsing(Invincible => new Invincible());
                s.WhenStarted(Invincible => Invincible.Start());
                s.WhenStopped(Invincible => Invincible.Stop());

                x.SetServiceName("InvincibleService");
                x.SetDisplayName("Invincible Service");
                x.SetDescription("Cerca di sopravvivere");

            });


        });

        int exitCodeValue = (int)Convert.ChangeType(exitCode, exitCode.GetTypeCode());
        Environment.ExitCode = exitCodeValue;
    }

thanks for the help.

aldo jaco
  • 5
  • 2

1 Answers1

0

Your code seems okay, even though you could do with a bit of a refactoring. For example move around this part like this:

var exitCode = HostFactory.Run(x =>
        {
            x.Service<Invincible>(s =>
            {
                s.ConstructUsing(Invincible => new Invincible());
                s.WhenStarted(Invincible => Invincible.Start());
                s.WhenStopped(Invincible => Invincible.Stop());
            });

            x.SetServiceName("InvincibleService");
            x.SetDisplayName("Invincible Service");
            x.SetDescription("Cerca di sopravvivere");
        });

Your issue is that, the windows service that you are running is using a different Windows user account, it's not the Windows user account which you use for logging in and using your PC. So their desktop paths are different. Every user account on Windows has a different desktop path.

To fix it, simply make the service log on using the account which you want the txt file to appear to.

enter image description here

Koray Elbek
  • 794
  • 5
  • 13
  • Thanks it works !!! is there a way to make it use the currently logged on user automatically ? – aldo jaco Aug 26 '21 at 10:56
  • Try to get the current logged on user using methods described here: [link](https://stackoverflow.com/questions/5218778/how-do-i-get-the-currently-logged-username-from-a-windows-service-in-net) and after that try to impersonate user following methods like these: [link](https://stackoverflow.com/a/38490877/10413298) Also, if the answer above helped, can you set it as the accepted answer? So that other users who have the same issue can try that also. – Koray Elbek Aug 26 '21 at 11:06