我有一个解决方案,开始在本地计算机上的服务,但我要做到以下几点:
1 - 连接到192.168.2.10机器。
2,检查,如果该服务已停止 - 运行用户ramy@domain.com和密码123
我的代码是:
所以请我需要帮助,使用指定的用户指定的电脑上运行此服务。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Security.Principal;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication2
{
public enum SimpleServiceCustomCommands
{ StopWorker = 128, RestartWorker, CheckWorker };
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
System.ServiceProcess.ServiceController[] scServices = System.ServiceProcess.ServiceController.GetServices();
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("MSSQLSERVER", "LOCALHOST");
richTextBox1.Text = "";
if (sc.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
sc.Start();
while (sc.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
sc.Refresh();
}
}
else if (sc.Status == System.ServiceProcess.ServiceControllerStatus.Paused)
{
sc.Start();
}
richTextBox1.Refresh();
}
private void button3_Click(object sender, EventArgs e)
{
System.ServiceProcess.ServiceController[] scServices = System.ServiceProcess.ServiceController.GetServices();
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("MSSQLSERVER", "LOCALHOST");
sc.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
// System.ServiceProcess.ServiceController.GetDataBaseHandleWithAccess(String machineName, Int32 serviceControlManaqerAccess)
System.ServiceProcess.ServiceController[] scServices = System.ServiceProcess.ServiceController.GetServices();
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("MSSQLSERVER", "LOCALHOST");
try
{
richTextBox1.Text += "Status = " + sc.Status + "\n";
richTextBox1.Text += "Can Pause and Continue = " + sc.CanPauseAndContinue + "\n";
richTextBox1.Text += "Can ShutDown = " + sc.CanShutdown + "\n";
richTextBox1.Text += "Can Stop = " + sc.CanStop + "\n";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
所有的感谢。