Hej folkens,
Jeg sidder her og leger med lidt programmering og får i følgende kode, en fejl jeg ikke forstår.
private void button1_Click(object sender, EventArgs e)
{
ServiceController controller = GetService();
ServiceControllerStatus MyStatus; //Enum
if (cbServices.Text != "<Select a service>" & cbServices.Text != string.Empty && controller.DisplayName == cbServices.Text)
{
serviceController1.DisplayName = cbServices.Text;
MyStatus = serviceController1.Status;
switch (MyStatus)
{
case ServiceControllerStatus.Running: StatusBar.Text = ("Service is running.");
break;
case ServiceControllerStatus.Stopped: StatusBar.Text = ("Service is stopped.");
break;
case ServiceControllerStatus.Paused: StatusBar.Text = ("Service is paused.");
break;
}
}
else
StatusBar.Text = ("There is no service by that name.");
}
Altså, der bliver tjekket for condition a og b, her efter (hvis de er opfyldt) for condition c.
Det fungere fint så længe "cbServices.Text" enten er "Empty", indeholder "<Select a service> eller et navn der eksisterer i "controller.DisplayName". Men skriver jeg f.eks. "test" går det i ged.
Nogle der gider kigge på det?
Her er koden i sin fulde længde hvis nogle skulle have lyst til at kompile skidtet.
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.ServiceProcess;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataTable _tblServices = new DataTable();
private void button1_Click(object sender, EventArgs e)
{
ServiceController controller = GetService();
ServiceControllerStatus MyStatus; //Enum
if (cbServices.Text != "<Select a service>" & cbServices.Text != string.Empty && controller.DisplayName == cbServices.Text)
{
serviceController1.DisplayName = cbServices.Text;
MyStatus = serviceController1.Status;
switch (MyStatus)
{
case ServiceControllerStatus.Running: StatusBar.Text = ("Service is running.");
break;
case ServiceControllerStatus.Stopped: StatusBar.Text = ("Service is stopped.");
break;
case ServiceControllerStatus.Paused: StatusBar.Text = ("Service is paused.");
break;
}
}
else
StatusBar.Text = ("There is no service by that name.");
}
private void Form1_Load(object sender, EventArgs e)
{
_tblServices.Columns.Add(new DataColumn("ServiceName", typeof(System.String)));
_tblServices.Columns.Add(new DataColumn("DisplayName", typeof(System.String)));
_tblServices.Columns.Add(new DataColumn("Index", typeof(System.Int32))); //used for sorting
System.ServiceProcess.ServiceController[] services;
//fill the array with the GetServices method
services = System.ServiceProcess.ServiceController.GetServices();
foreach (System.ServiceProcess.ServiceController controller in services)
{
DataRow row = _tblServices.NewRow();
row["ServiceName"] = controller.ServiceName;
row["DisplayName"] = controller.DisplayName;
row["Index"] = 1;
_tblServices.Rows.Add(row);
}
DataRow blankRow = _tblServices.NewRow();
blankRow["ServiceName"] = string.Empty;
blankRow["DisplayName"] = "<Select a service>";
blankRow["Index"] = 0;
_tblServices.Rows.Add(blankRow);
DataView view = new DataView(_tblServices, string.Empty, "Index, DisplayName", DataViewRowState.CurrentRows);
cbServices.DataSource = view;
cbServices.ValueMember = "ServiceName";
cbServices.DisplayMember = "DisplayName";
}
private ServiceController GetService()
{
if (cbServices.Text.ToString().Trim() != string.Empty)
return new ServiceController((string)cbServices.Text);
else
return null;
}
}
}
Indlæg senest redigeret d. 15.08.2008 10:51 af Bruger #14083