using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.ServiceProcess;
using System.Windows.Forms;
using System.Configuration.Install;
namespace logoutNAS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
ServiceController tController = new ServiceController();
tController.MachineName = ".";
tController.ServiceName = "LanmanWorkstation";
TimeSpan timeout = TimeSpan.FromMilliseconds(1000 * 10);
// 設定一個 Timeout 時間,若超過 10 秒啟動不成功就宣告失敗!
if (tController.Status != ServiceControllerStatus.Stopped && tController.Status != ServiceControllerStatus.StopPending)
{
tController.Stop();
//tController.Close();
// 設定該服務必須在等待 timeout 時間內將狀態改變至「已關閉(Stopped)」的狀態
tController.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
}
catch //(Exception ex)
{
throw new InstallException("服務無法停用");
}
try
{
ServiceController tController = new ServiceController();
tController.MachineName = ".";
tController.ServiceName = "LanmanWorkstation";
TimeSpan timeout = TimeSpan.FromMilliseconds(1000 * 10);
tController.Start();
tController.WaitForStatus(ServiceControllerStatus.Running, timeout);
//MessageBox.Show("已啟動服務");
label1.Text = "已登出";
}
catch
{
System.Windows.Forms.MessageBox.Show("服務無法啟動");
}