How to Create a Windows Service Using C# and Visual Studio!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up you guys welcome back to the channel in today's video I'm gonna do something a little different I'm going to show you how to create a Windows service in c-sharp code using visual studio and the.net framework [Music] so this is something I was doing earlier in the week uh for work and so I thought I'd make a useful video because I know a lot of people are curious about making and setting up Windows services and in visual studio with c-sharp and the.net framework is kind of the easiest way to make a Windows service that I've found personally and so to do this let's just get started let's dive right into it open up your visual studio and we're gonna do uh create a new project and search for templates and just say windows service in here and there should be an option for windowservice with.net framework like you can see right here so just go ahead and make one of those call it whatever you want the project name to be I'll call it a Windows service tutorials hit create what you're going to see is we really only need to modify slash add like two three lines of code and then run the like service installer to do this entire thing um so what should boot up for you hopefully is the same screen that I have right now and I know it's going to be kind of small especially if you're watching this on a phone but basically this whole middle screen is Service uh whatever.cs service1.cs just right click somewhere in that middle and hit add installer um so essentially that's gonna just create all this stuff that you can maybe see on your right it's going to create this project installer file and so it's going to take you to project installer.cs and then design right in here but your solution Explorer now has underneath project installer hopefully project installer.designer.cs okay so that's good but now inside of this file we want to right click and we want to hit view code you can also hit the F7 key which is a shortcut but we're going to go to view code and hopefully you have exactly this already generated for you you uh that should be what you're seeing if not you can pause and type in all these lines I'm not going to go through what all of them do just understand C sharp and visual studio are giving you the backbone of what you need to create a Windows service okay so in here we don't have to change anything right now but click on the initialize component um uh call right here right click and then we're going to go to the definition so that's F12 which is the shortcut or you can just click here go to definition and now it should take you into something quite a bit longer here where that has a lot more going on and you can see the core definition of initialize component and so all we're going to add in here are a few things that don't come by default so underneath the service process installer one but right above the password and username and this is also where you could give it credentials that it needs to prompt you for to start or end the service I'm going to leave them as null because for this tutorial I don't want to put credentials on it but one line we are going to add is this dot service process installer one dot and then account with a capital A equals and then system dot service process dot service account dot local system and this is basically saying that and make sure you spell everything right this is basically just saying that I want the account associated with this service to be whatever's actively logged in so you're just making a very easy shortcut to say hey uh whoever is logged in can run this service um and now right above this where it says um the service name is going to be service one which is just kind of the default let's add a few things we're going to add a description and a display name and to do that it's going to be this dot service installer one dot description and when it comes time to install or make sure you spell it right and when it comes time to see like well how are these actually used that's going to come up when we actually install it on our machine and start running it so you're going to see whatever you put here you're going to see it again later so I'm going to call it Windows service tutorial just like that because that's what I named the whole thing don't forget your semicolons at the end and then this dot same deal service installer1 Dot and then this time instead of description it's going to be display name with a capital D and a capital N and uh just put in here whatever you want to show up on the left side so I'll say win serve toot just like that is fine and I could even do like dot demo or something like that you're gonna see these again um so just understand that's kind of an optional step but you have to have some description and some display name so pick something that kind of makes sense for your service and whatever you want to have it do okay so now we're going to go and add a timer and the actual code that's going to do our service and I'm not going to dive too much into the actual c-sharp code in this video if you are looking for a Windows service to do something specific let me know in the comments what you want to see a service doing and I could make a specific uh tutorial on how to write that C sharp code this specifically is a tutorial on how to create that Windows service so let's keep rolling now in your system tree under your project installer CS you should see service1.cs as well as service1.designer.cs and so this time just double click on or it might be open up here just go to service one dot CS okay all right so the time has come for the actual code that is going to run inside of our service so telling the service what to do when we install it and run it and I don't want to spend a ton of time on this because my service is just going to say what time the service was started every five seconds while it's running take a time stamp of the time and save that to the file and then tell you in the log file what time you stop the service so it's not a super useful service there's not really a practical application of it so I'm going to bring in that code I'll quickly talk you through it but then we'll focus on how to make the windows service installed and running okay so just to quickly talk you through it the only two modules I have to add into the using section is timers and system.io everything else is just the standard ones that come with it and I set up a timer variable because I am going to be using that five second interval for the time stamps um so I need one of those you don't necessarily need a timer if your service isn't doing anything on a clock but then the pre-built two functions it gives you are what to do on start and what to do on stop and I'm going to use this function that I create right to file to just say service is started at and then the built-in function to capture whatever the date and time is at that time but then I start the time elapsed and tracking my interval functionality in the onstart we don't need anything fancy like that on stop because we're shutting it down but so we had to set up a function to tell it what to do when elapsed time has finished and to do that I just basically say Services recall or service you can think of this as was ran at whatever time it currently is on your computer clock then and then the last thing we need to do is actually set up this right to file function so this was the most complicated piece in my function but it's still not too complicated to talk through it basically all we had to do was say the path we want to store this log file in is wherever I have my project in so whatever C sharp uh folder we're building this in forward slash logs or backslash logs if that directory does not exist we're gonna make it and then if the file service log underscore whatever the date is for this date then we want to create that file as well but if the file does exist we just want to write our message to the line so it is super straightforward you don't have to worry too much about what my code is doing but now I have something here that is supposed to tell you in a log what time it was started and then every five seconds log what's happening and then tell you what time has stopped so I have a service that will do something once we build it and install it okay so assuming we did everything right then we can come over to our system Tree on the right and wherever we have the title so the C sharp block windows service tutorial or whatever you called yours just right click and go with rebuild okay hopefully you get no issues found that would be great that is the goal um and then uh if that is what uh worked for you if that is what happened then you can go to uh your computer and open up command prompt and make sure to run it as an administrator just in case you need any level of security to add services to whatever machine you're working on what we want to do is we want to change the directory to the directory that we're working in and so for this it's a pretty simple command CD and then the directory that we want to change it to so C colon backslash Windows backslash microsoft.net backslash framework backslash and then whatever version you're using I'm using 4.0.30319 and once you hit enter it should show you that that's what you're getting all right now to actually run the install uh utility the command for that looks like this install util and then we need to uh dot exe and then we need to add in the entire path so you don't need this plus but you need entire path of where your executable lives so right click on your build again within Visual Studio open file in file explorer open folder and file explorer and then navigate to bin and then debug and then your executable should be in here type application and so Windows service tutorial so what you want to do is you want to copy this entire path into that command prompt just like that and then um so I'll try and make that a little bigger for you and then you need to add on to here the name of your service so this was Windows oop capital I Windows service tutorial dot exe just like that okay and so that should be all that you have to do to actually get it to show up on your services Loadout so when I hit enter if we did everything right you get this really quick blast through of it creating the service and putting it on your computer and then you should have the commit phase completed successfully the transacted install has completed if you get an error message at this point double and triple check that pathing so it needs to be installutil.exe and then the entire path to direct it into your project Dot and then backslash bin backslash debug backslash the name of your service.exe that should be all you need to run now we have put the service onto our services.msc file but it's not running okay so let's go ahead and uh open up the um run window and just type in services.msc okay so that's going to open the services window most of you can get here if you just type Services into the Windows key but this makes us feel like more more of a hacker right so uh okay let's go down until we find what we just named our service and so here remember I said before whatever we put down in our um in our name and description is going to be important now so again this is going to be let's go ahead to the definition of initialize component so we are looking for this Windows service tutorial okay so here you can see when serve toot dot demo which is what we put in display name is here in the name column and then what we put in description which was Windows service tutorial is here in the description folder uh uh column excuse me and so what we can do is if we right click on this now and we tell it to start it's going to go ahead and start up and assuming we wrote Rock Solid code which I'm sure whatever you want your service to do it'll it'll be running just fine now if what what's really encouraging for mine is that it creates a logs folder and a file if it didn't already exist so I'm going to open this thing up and what you'll see when I pull this over here for you oh when I pull this over here for you is that I am getting an entry every five seconds and it tells me what time I started up the service and now if I go ahead and I stop this service stop the service okay now let me close the Old Log file and open the new log file so now you can see it gave me an entry every five seconds while it was running and then once it stopped uh it just sent an entry for what time it stopped at so again this is really just a super useful tutorial for any kind of Windows service that you want to set up but if you do have a specific question on how to create a specific service to do a specific task please do let me know about in the comments below I will try to make a few of them if there's enough interest in some of these different concepts okay so I think the last really valuable thing we could take a look at together is how to uninstall a service so if you have created a service that you regret or it's outlived its usefulness and you want to get rid of it it is really similar to how we installed okay so just like before we need to make sure that we change our working directory so CD c colon backslash Windows backslash Microsoft microsoft.net net and then backslash framework and then backslash V 4.0.30319 or whatever version you're working with um now we go back to wherever our executable is and do debug and uh the command is going to look very similar to the install command because it's actually the same tool it's the install utility.exe but you need to add in this Dash U that tells it to uninstall now and then copy in that same path like before and then add in the windows service tutorial the name of the service you're uninstalling just like before and then add dot exe to the end hit enter it should tell you removing is being removed successfully removed uninstall is completed those are the the messages that you're looking for again the most common issue you'll get here um if it does throw an error for you is your file path will be looking incorrect or maybe you forgot that minus you um but so double check your file path be really meticulous about that but now if we go to Windows service you can see it's still there because um this doesn't refresh while I have it open but I'm going to go ahead and refresh the entire Services thing and you can see it's gone now so that is how you get rid of Windows services that maybe have outlived their usefulness or are doing more harm than good okay so I hope that this tutorial provides you the basics of how to create a Windows service using c-sharp and visual studio using the windows.net framework I know that it is a somewhat complicated thing and it Strays a little away from the usual like friendly python content on the channel but I had to do one of these for work and I honestly just thought it would be a great tutorial for the channel so be sure to leave a like if you found this useful subscribe to the channel for a ton more great content let me know in the comments below what you'd like to see more of on the channel or what you'd like to see next and as always thank you guys for watching and until next time bye
Info
Channel: LeMaster Tech
Views: 47,414
Rating: undefined out of 5
Keywords: create a windows service in c#, c# windows service, how to make a windows service, windows services, windows services c# tutorial, c# tutorial for beginners, learning C#, c# windows application, c# windows service tutorial
Id: 67DtZbPYNKg
Channel Id: undefined
Length: 17min 13sec (1033 seconds)
Published: Mon May 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.