Making Sure A Service Doesn't Start For Other Users

Posted on 2009-02-16
Last Modified: 2013-12-17
Hey everyone

I have created a service for myself to start various processes upon logon.  I know that I could have just added a registry value, but I fancied doing it via code, that way I can take the service with me and create logs etc.

What I don't want is for the process to start for anybody other than myself, so I thought about putting the following code in the OnStart method:

if (WindowsIdentity.GetCurrent().Name.ToString() == myUserName)

However when I done a quick test on a normal Windows App I found that the form would initially load, then exit.  As there is a good deal of processing within my code I really dont want it to do anything unless it's me who is logged in.  Is there anyway that I can make sure that it won't activate any other code unless I'm the logged in user?
Question by:angus_young_acdc
    LVL 19

    Accepted Solution

    Services start when the pc/server boots up and wil run even if non one logs on.  If they didn't work like this then thye would be no good and nothing would work!
    With this in mind does your question make any sense?
    LVL 15

    Author Comment

    I think it does.  I want it to automatically run when the PC boots, I just don't want any of the processing to happen unless its me that has logged in.
    LVL 19

    Assisted Solution

    Ok I see.
    In that case your service should start and then start an infinite loop where it periodically checks the logged on user and if it is you then go and run whatever code.
    Would that suit your needs?

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now