C# window服务操作
public int GetWindowsServiceStartType(String sServiceName) //判断服务状态是手动还是禁用还是自动
{
string sState = ""; try
{
System.Diagnostics.ProcessStartInfo objProcessInf = new System.Diagnostics.ProcessStartInfo(); objProcessInf.FileName = "cmd.exe"; objProcessInf.UseShellExecute = false; //是否使用系统shell启动进程 objProcessInf.RedirectStandardError = true;//错误信息写入流
objProcessInf.RedirectStandardInput = true;
objProcessInf.RedirectStandardOutput = true; //应用程序的输出写入流 objProcessInf.CreateNoWindow = true; //是否在新窗口启动该进程 objProcessInf.Arguments = "/c sc qc " + sServiceName; //启动参数 System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(objProcessInf); //启动进程 String sStateValue = objProcess.StandardOutput.ReadToEnd(); //读取应用程序输出的流 if (sStateValue.IndexOf("AUTO_START") > )
{
sState = "AUTO_START";
return ;
} if (sStateValue.IndexOf("DEMAND_START") > )
{
sState = "DEMAND_START";
return ;
} if (sStateValue.IndexOf("DISABLED") > )
{
sState = "DISABLED";
return ;
} return ; }
catch (Exception e)
{
sState = e.Message;
return -;
}
} public Boolean SetWindowsServiceStartType(String sServiceName, int iStartType) //设置服务状态是手动,自动,还是禁用
{
try
{
System.Diagnostics.ProcessStartInfo objProcessInf = new System.Diagnostics.ProcessStartInfo(); objProcessInf.FileName = "cmd.exe"; objProcessInf.CreateNoWindow = false;
objProcessInf.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; string sStartState = "boot"; switch (iStartType)
{
case :
{
sStartState = "system";
break;
}
case :
{
sStartState = "auto";
break;
}
case :
{
sStartState = "demand";
break;
}
case :
{
sStartState = "disabled";
break;
}
default:
{
break;
}
} objProcessInf.Arguments = "/c sc config " + sServiceName + " start= " + sStartState; System.Diagnostics.Process.Start(objProcessInf); return true;
}
catch
{
return false;
}
}
ServiceController sc = new ServiceController("Themes");
if (sc.Status.Equals(ServiceControllerStatus.Running))
{
sc.Stop();
sc.Refresh();
}
if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
sc.Start();
sc.Refresh();
}
bool exist = false;
ServiceController[] Services = ServiceController.GetServices();
for (int i = ; i < Services.Length; i++)
{
if (Services[i].DisplayName.ToString() == textBox1.Text.Trim())
exist = true;
}
return exist;
C# window服务操作的更多相关文章
- 使用winform程序控制window服务的操作
继上篇 c#之添加window服务(定时任务) 基础之上, 这篇文章主要讲述,使用winform程序来控制window服务的安装,启动,停止,卸载等操作 1.在同一个解决方案添加winform项目,如 ...
- C# 编写Window服务基础(一)
一.Windows服务介绍: Windows服务以前被称作NT服务,是一些运行在Windows NT.Windows 2000和Windows XP等操作系统下用户环境以外的程序.在以前,编写Wind ...
- 自定义Window 服务
自定义window 服务 开发到使用的流程: 1.完成对应的代码之后(代码在底下),右键MyService.cs 添加安装程序 2.添加window服务安装程序打开Service1.cs[设计]页面, ...
- 【权限维持】window服务端常见后门技术
0x00 前言 未知攻焉知防,攻击者在获取服务器权限后,通常会用一些后门技术来维持服务器权限,服务器一旦被植入后门,攻击者如入无人之境.这里整理一些window服务端常见的后门技术,了解攻击者的常见后 ...
- C#写的window服务内存溢出
浅谈c#垃圾回收机制(GC) 写了一个window服务,循环更新sqlite记录,内存一点点稳步增长.三天后,内存溢出.于是,我从自己的代码入手,查找到底哪儿占用内存释放不掉,最终明确是调用servi ...
- 接Window服务(二)
接Window服务(一) ServiceController方法调用 1 public partial class Service1 : ServiceBase 2 { 3 public Servic ...
- window 服务(一)
windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志.计算机启动时,服务会自动开始 ...
- window 服务(二)
接Window服务(一) ServiceController方法调用 public partial class Service1 : ServiceBase { public Service1() { ...
- c# window服务-初学习
window服务-初学习 一.工具: VS2015+NET Framework4.5. 二.操作: 1.新建windows服务的项目: 2.修改windows服务相关内容: 3.预览windows服务 ...
随机推荐
- AtCoder ARC 076E - Connected?
传送门:http://arc076.contest.atcoder.jp/tasks/arc076_c 平面上有一个R×C的网格,格点上可能写有数字1~N,每个数字出现两次.现在用一条曲线将一对相同的 ...
- 10.Spring注解+MyBatis+Servlet
1.创建sql脚本 create table account ( id ) unsigned not null auto_increment primary key, account_number ) ...
- 【Codeforces Round #519 by Botan Investments C】 Smallest Word
[链接] 我是链接,点我呀:) [题意] [题解] 模拟了一两下.. 然后发现. 对于每一个前缀. 组成的新的最小字典序的字符串 要么是s[i]+reverse(前i-1个字符经过操作形成的最大字典序 ...
- uva 交叉染色法10004
鉴于网上讲交叉染色的资料比较少,于是我把我自己的心得与方法贴出来,方便与大家共同进步. 二分图: 百度百科传送门 wiki百科传送门 判断一个图是否为二分图可以用交叉染色的方法来判断,可以用BFS,也 ...
- Linux用户管理之使用/bin/false和/usr/sbin/nologin拒绝用户登录及其功能分析(转)
/bin/nologin,/bin/false的意思是禁止某个用户登录. 比较常用的用法: #添加一个不能登录的用户 useradd -d /usr/local/apache -g apache -s ...
- HDU3117-Fibonacci Numbers(矩阵高速幂+log)
题目链接 题意:斐波那契数列,当长度大于8时.要输出前四位和后四位 思路:后四位非常easy,矩阵高速幂取模,难度在于前四位的求解. 已知斐波那契数列的通项公式:f(n) = (1 / sqrt(5 ...
- jquery非文本框复制
function selectText(x) { if (document.selection) { var range = document.body.createTextRange();//ie ...
- 【编码格式错误】SyntaxError: Non-UTF-8 code starting with
问题: SyntaxError: Non-UTF-8 code starting with '\xba' in file E:/placement/placement/Placement Test/c ...
- android-PullRefreshLayout使用
类似与谷歌的SwipeRefreshLayout.但比SwipeRefreshLayout美丽. 执行效果: 使用说明: 用法和SwipeRefreshLayout几乎相同. 在xml中: 1 2 3 ...
- 第一次Java作业——简单的登录界面
千里之行,始于足下,从小做起,一点一滴学编程. import javax.swing.*; import java.awt.*; public class Homework{ public stati ...