Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm going to run a exe file in C# windows service but the service does not start and I get an error!
I also test for writing in a text file in this way and that is OK!
Can you help me???
Thanks.


What I have tried:

I have tried this codes:
C#
private void LogService(string content)
       {
           if (!ProgramRun)
               RunProgram();
           //
           FileStream fs = new FileStream(@"d:\TestServiceLog.txt", FileMode.OpenOrCreate, FileAccess.Write);
           StreamWriter sw = new StreamWriter(fs);
           sw.BaseStream.Seek(0, SeekOrigin.End);
           sw.WriteLine(content);
           sw.Flush();
           sw.Close();
       }

       private void RunProgram()
       {
           ProgramRun = true;
           string ProgramPath = @"\ScreenSaverSample1\bin\Debug\ScreenSaverSample1.exe";
           string startupPath = Environment.CurrentDirectory;
           string projectDirectory = Directory.GetParent(startupPath).Parent.Parent.FullName + ProgramPath;
           // Console.WriteLine(projectDirectory);
           Process p = new Process();
           p.StartInfo.FileName = projectDirectory;
           //   p.StartInfo.Arguments = "/c dir *.cs";
           p.StartInfo.UseShellExecute = false;
           p.StartInfo.RedirectStandardOutput = true;
           p.Start();
           string output = p.StandardOutput.ReadToEnd();
           p.WaitForExit();
           // Console.WriteLine("Exited!");
           ProgramRun = false;
       }
Posted
Updated 3-Dec-19 18:04pm

Services can't have any user interface, or run an app that does - and your app name implies it accesses the display.
Windows Services cannot start additional applications because they are not running in the context of any particular user. Unlike regular Windows applications, services are run in an isolated session and are prohibited from interacting with a user or the desktop. This leaves no place for the application to be run.

I don't know what you are trying to do, but it's not going to work!
 
Share this answer
 
Comments
Mohammad Tavoosi 3-Dec-19 18:11pm    
I want to run an executable file automatically after the computer is turned on. What is your suggestion?
OriginalGriff 4-Dec-19 2:11am    
You can't: you can only run Services (which have no UI at all) until the user is logged in, because until then there is no desktop, no user to interact with. You can't run a screensaver over the login screen because there is no desktop for it to "replace".

This behaviour is by design: it's a security feature to prevent "login stealing" apps putting up a screen pretending to be the login screen, capturing the login details and then closing as if the details were wrong.
How you do this depends on the application you want to run.

If it shows any user interface at all, it cannot run when Windows starts. There is no user desktop for the application to show itself on so no way to run it. Only services run when Windows starts.

Applications that the user can see must start when the user logs in. You can do that by either launching the app from the scheduler or by putting the command line to launch the app in the registry under the HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run key. Whenever a user logs in, all of the commands under this key are executed, usually launching applications.

A screen saver, since it interacts with the desktop, can NOT be launched from a service. You can read up more on Windows Sessions and Desktops here[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900