Start stop Service from Form App c#

后端 未结 5 1941
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 19:11

How can I start and stop a windows service from a c# Form application?

5条回答
  •  Happy的楠姐
    2020-12-04 20:05

    there is a dirtier, but same same..
    just execute the shell command

    NET STOP "MYSERVICENAME"
    NET START "MYSERVICENAME"
    

    EDIT: Would like to expand on "dirtier":
    1. it is slower.
    2. this can result in some clever vulnerabilities
    3. it is a code that is not "understandable"
    4. it is highly not portable (surely if you use .NET Core)

    I am sure there are more wrong with it...
    but if you search for a small, inhouse tool... it'll do.
    (would like to say temporary, but temp stuff stick best!)

提交回复
热议问题