Hi All,
With the following code I write running processes to a listbox:
------------------------------------------------------------
Process[] procs;
private void Getprocs()
{
procs = Process.GetProcesses();
if (Convert.ToInt32(label2.Text) != procs.Length)
{
listBox1.Items.Clear();
for (int i = 0; i < procs.Length; i++)
{
listBox1.Items.Add(procs[i].MainWindowTitle);
}
label2.Text = procs.Length.ToString();
}
}
---------------------------------------------------------
The problem is the I ONLY want to write those processes to the list where the Process.MainWindowTitle property does not match "nothing".
To find this out i use: (procs[i].MainWindowTitle != "")
How can I incoporate this check with my code ?
Thanks !
Loading
minaajPosted Nov 18, 2010, 5:45 AM
The listbox is now populated with only the processes who's MainWindowTitle do not match null by altering my original code with this:
-------------------------------------------------------------------
Process[] procs;
private void Form1_Load(object sender, EventArgs e)
{
Getprocs();
}
private void Getprocs()
{
procs = Process.GetProcesses();
if (Convert.ToInt32(label2.Text) != procs.Length)
{
listBox1.Items.Clear();
for (int i = 0; i < procs.Length; i++)
{
if (procs[i].MainWindowTitle != "")
{
listBox1.Items.Add(procs[i].MainWindowTitle);
}
else
{
}
}
label2.Text = procs.Length.ToString();
}
}
-----------------------------------------------------------------------
The final problem, which seems to be caused by above, is that my kill process method now gives me an "Access Denied" while it used to be able to kill the selected process with the following:
---------------------------------------------------------------
private void endProcessToolStripMenuItem_Click(object sender, EventArgs e)
{
procs[listBox1.SelectedIndex].Kill();
}
----------------------------------------------------------------
Anyone have any thoughts on that ?
Thanks !
ShankeyPosted Nov 18, 2010, 5:10 AM
You can use if (String.IsNullOrEmpty(procs[i].MainWindowTitle) == false)
Madhu KPosted Nov 18, 2010, 5:06 AM
for (int i = 0; i < procs.Length; i++)
{
if(procs[i].MainWindowTitle != "")
{
listBox1.Items.Add(procs[i].MainWindowTitle);
}
else{//nothing}
}