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

Madhu KRank #1,514 · 1.1K reputationPosted Nov 18, 2010, 12:06 AM EST
for (int i = 0; i < procs.Length; i++)
{
if(procs[i].MainWindowTitle != "")
{
listBox1.Items.Add(procs[i].MainWindowTitle);
}
else{//nothing}
}
ShankeyRank #972 · 2.1K reputationPosted Nov 18, 2010, 12:10 AM EST
You can use if (String.IsNullOrEmpty(procs[i].MainWindowTitle) == false)
minaaj9 reputationPosted Nov 18, 2010, 12:45 AM EST
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 !