windows-services

Install a .NET windows service without InstallUtil.exe

雨燕双飞 提交于 2019-11-26 03:36:32
问题 I have a standard .NET windows service written in C#. Can it install itself without using InstallUtil? Should I use the service installer class? How should I use it? I want to be able to call the following: MyService.exe -install And it will have the same effect as calling: InstallUtil MyService.exe 回答1: Yes, that is fully possible (i.e. I do exactly this); you just need to reference the right dll (System.ServiceProcess.dll) and add an installer class... Here's an example: [RunInstaller(true)

Map a network drive to be used by a service

为君一笑 提交于 2019-11-26 03:15:42
问题 Suppose some Windows service uses code that wants mapped network drives and no UNC paths. How can I make the drive mapping available to the service\'s session when the service is started? Logging in as the service user and creating a persistent mapping will not establish the mapping in the context of the actual service. 回答1: You'll either need to modify the service, or wrap it inside a helper process: apart from session/drive access issues, persistent drive mappings are only restored on an

How do you retrieve a list of logged-in/connected users in .NET?

陌路散爱 提交于 2019-11-26 02:57:27
问题 Here\'s the scenario: You have a Windows server that users remotely connect to via RDP. You want your program (which runs as a service) to know who is currently connected. This may or may not include an interactive console session. Please note that this is the not the same as just retrieving the current interactive user. I\'m guessing that there is some sort of API access to Terminal Services to get this info? 回答1: Here's my take on the issue: using System; using System.Collections.Generic;

How to install a windows service programmatically in C#?

馋奶兔 提交于 2019-11-26 02:40:56
问题 I have 3 projects in my VS solution. One of them is a Web app, the second one is a Windows Service and the last one a Setup project for my Web app. What I want is by the end of the installation of the web app in my setup project, within my custom action to try and install my windows service given that I have the location of the assembly by then. 回答1: I found several errors in the code that you reused and have fixed these and also cleaned it up a little. Again, the original code is taken from

How can I run an EXE program from a Windows Service using C#?

空扰寡人 提交于 2019-11-26 02:34:04
问题 How can I run an EXE program from a Windows Service using C#? This is my code: System.Diagnostics.Process.Start(@\"E:\\PROJECT XL\\INI SQLLOADER\\ConsoleApplication2\\ConsoleApplication2\\ConsoleApplication2\\bin\\Debug\\ConsoleApplication2.exe\"); When I run this service, the application is not starting. What\'s wrong with my code? 回答1: This will never work , at least not under Windows Vista or later. The key problem is that you're trying to execute this from within a Windows Service, rather

How can I verify if a Windows Service is running

≡放荡痞女 提交于 2019-11-26 02:28:05
问题 I have an application in C# (2.0 running on XP embedded) that is communicating with a \'watchdog\' that is implemented as a Windows Service. When the device boots, this service typically takes some time to start. I\'d like to check, from my code, if the service is running. How can I accomplish this? 回答1: I guess something like this would work: Add System.ServiceProcess to your project references (It's on the .NET tab). using System.ServiceProcess; ServiceController sc = new ServiceController

What directory does a Windows Service run in?

不打扰是莪最后的温柔 提交于 2019-11-26 02:22:36
问题 I\'ve created a very simple .NET Windows Service and installed it using InstallUtil.exe utility. In the service I have a piece of code as such: if (File.Exists(\"test_file.txt\")) { // Do something clever } I\'ve created a file called test_file.txt in the same directory as the service but the commented part of the code is never being executed...? 回答1: Services are started from an application called Service Control Manager. This application lives in the system directory %WinDir%\System32 On a

windows service vs scheduled task

偶尔善良 提交于 2019-11-26 02:17:15
问题 What are the cons and pros of windows services vs scheduled tasks for running a program repeatedly (e.g. every two minutes)? 回答1: Update: Nearly four years after my original answer and this answer is very out of date. Since TopShelf came along Windows Services development got easy. Now you just need to figure out how to support failover... Original Answer: I'm really not a fan of Windows Scheduler. The user's password must be provided as @moodforall points out above, which is fun when someone

How might I schedule a C# Windows Service to perform a task daily?

五迷三道 提交于 2019-11-26 01:42:34
问题 I have a service written in C# (.NET 1.1) and want it to perform some cleanup actions at midnight every night. I have to keep all code contained within the service, so what\'s the easiest way to accomplish this? Use of Thread.Sleep() and checking for the time rolling over? 回答1: I wouldn't use Thread.Sleep(). Either use a scheduled task (as others have mentioned), or set up a timer inside your service, which fires periodically (every 10 minutes for example) and check if the date changed since

Create Windows service from executable

假如想象 提交于 2019-11-26 01:34:27
问题 Is there any quick way to, given an executable file, create a Windows service that, when started, launches it? 回答1: To create a Windows Service from an executable, you can use sc.exe : sc.exe create <new_service_name> binPath= "<path_to_the_service_executable>" You must have quotation marks around the actual exe path, and a space after the binPath= . More information on the sc command can be found in Microsoft KB251192. Note that it will not work for just any executable: the executable must