4.2 设置服务启动方式为自动启动
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
this.Committed += new InstallEventHandler(ProjectInstaller_Committed);
}
private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
//参数为服务的名字
System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("ServiceSapSyn");
controller.Start();
}
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
}
}
步骤五、脚本配置。
安装服务脚本
复制代码 %SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe WindowsServiceTest.exeNet Start ServiceTestsc config ServiceTest start= auto
卸载服务脚本
复制代码 %SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe /u WindowsServiceTest.exe

5.1 停止或启动服务的代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string thispath = Application.StartupPath;
public string Propath = "";
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "启动服务";
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
string StarPath = @"%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe " + Propath;
FileStream fs = new FileStream(thispath + "Install.bat", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine(StarPath);
sw.WriteLine("Net Start ServiceTest");
sw.WriteLine("sc config ServiceTest start= auto");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
sw.Close();
fs.Close();
}
System.Diagnostics.Process.Start(thispath + "Install.bat");
this.Text = "启动服务:你选择的服务已经启动。";
Cursor = Cursors.Default;
}
/// <summary>
/// 停止服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
string StarPath = @"%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe /u " + Propath;
FileStream fs = new FileStream(thispath + "Uninstall.bat", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine(StarPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
sw.Close();
fs.Close();
}
System.Diagnostics.Process.Start(thispath + "Uninstall.bat");
this.Text = "启动服务:你选择的服务已经卸载。";
Cursor = Cursors.Default;
}
private void button3_Click(object sender, EventArgs e)
{
///选择文件框 对象
OpenFileDialog ofd = new OpenFileDialog();
//打开时指定默认路径
ofd.InitialDirectory = @"C:Documents and SettingsAdministrator.ICBCOA-6E96E6BE桌面";
//如果用户点击确定
if (ofd.ShowDialog() == DialogResult.OK)
{
//将用户选择的文件路径 显示 在文本框中
textBox1.Text = ofd.FileName;
Propath = textBox1.Text;
}
if (File.Exists(thispath + "Uninstall.bat"))
{
File.Delete(thispath + "Uninstall.bat");
}
File.Create(thispath + "Uninstall.bat").Close();
if (File.Exists(thispath + "Install.bat"))
{
File.Delete(thispath + "Install.bat");
}
File.Create(thispath + "Install.bat").Close();
}
//读写文本 - 写入数据按钮
private void buttonWrite_Click(string filePath)
{
}
/// <summary>
/// 运行CMD命令
/// </summary>
/// <param name="cmd">命令</param>
/// <returns></returns>
public static string Cmd(string[] cmd)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.AutoFlush = true;
for (int i = 0; i < cmd.Length; i++)
{
p.StandardInput.WriteLine(cmd[i].ToString());
}
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
return strRst;
}
/// <summary>
/// 关闭进程
/// </summary>
/// <param name="ProcName">进程名称</param>
/// <returns></returns>
public static bool CloseProcess(string ProcName)
{
bool result = false;
System.Collections.ArrayList procList = new System.Collections.ArrayList();
string tempName = "";
int begpos;
int endpos;
foreach (System.Diagnostics.Process thisProc in System.Diagnostics.Process.GetProcesses())
{
tempName = thisProc.ToString();
begpos = tempName.IndexOf("(") + 1;
endpos = tempName.IndexOf(")");
tempName = tempName.Substring(begpos, endpos - begpos);
procList.Add(tempName);
if (tempName == ProcName)
{
if (!thisProc.CloseMainWindow())
thisProc.Kill(); // 当发送关闭窗口命令无效时强行结束进程
result = true;
}
}
return result;
}
}










