G
garyusenet
Hello everyone I hope you are having a good evening.
This evening I made a method that allows me to find the process id's of
running processes of a given 'friendly name' - using
System.Diagnostic.Process.
Here is my code that works fine: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace ProcessFun
{
public partial class Form1 : Form
{
Process[] ProcessArray = ProcessesToArray();
public Form1()
{
InitializeComponent();
}
private Process[] ProcessesToArray()
{
Process[] ProcessArray =
Process.GetProcessesByName("someprocess");
return ProcessArray;
}
private void DisplayProcessID(Process[] procArray)
{
if (procArray.Length == 1)
{
MessageBox.Show(procArray[0].Id.ToString());
return;
}
if (procArray.Length > 1)
{
string procIDs = "";
foreach(Process p in procArray)
{
procIDs += "\n" + p.Id.ToString();
}
MessageBox.Show(procIDs);
return;
}
MessageBox.Show("No Processes are running!");
return;
}
private void Form1_Load(object sender, EventArgs e)
{
DisplayProcessID(ProcessArray);
}
}
}
I thought it would be better to declare my process Array after the
class declaration so that any methods that are part of the class will
have direct access to it. But when I moved the declaration to
underneath the class declaration, so that the programme now started
like so: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace Daplayaround
{
public partial class Form1 : Form
{
Process[] ProcessArray = ProcessesToArray();
public Form1()
{
InitializeComponent();
}
....
The compiler told me I was not permitted to do this.
----------------------
Error 1
A field initializer cannot reference the nonstatic field, method, or
property...
----------------------
What have I done wrong here?
Also would you please comment on the overall decency of the first piece
of code I pasted, and if i should be using a different nameing
convention or style please let me know. As I am new to this, but trying
to learn things properly.
Thankyou all,
Gary-
This evening I made a method that allows me to find the process id's of
running processes of a given 'friendly name' - using
System.Diagnostic.Process.
Here is my code that works fine: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace ProcessFun
{
public partial class Form1 : Form
{
Process[] ProcessArray = ProcessesToArray();
public Form1()
{
InitializeComponent();
}
private Process[] ProcessesToArray()
{
Process[] ProcessArray =
Process.GetProcessesByName("someprocess");
return ProcessArray;
}
private void DisplayProcessID(Process[] procArray)
{
if (procArray.Length == 1)
{
MessageBox.Show(procArray[0].Id.ToString());
return;
}
if (procArray.Length > 1)
{
string procIDs = "";
foreach(Process p in procArray)
{
procIDs += "\n" + p.Id.ToString();
}
MessageBox.Show(procIDs);
return;
}
MessageBox.Show("No Processes are running!");
return;
}
private void Form1_Load(object sender, EventArgs e)
{
DisplayProcessID(ProcessArray);
}
}
}
I thought it would be better to declare my process Array after the
class declaration so that any methods that are part of the class will
have direct access to it. But when I moved the declaration to
underneath the class declaration, so that the programme now started
like so: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace Daplayaround
{
public partial class Form1 : Form
{
Process[] ProcessArray = ProcessesToArray();
public Form1()
{
InitializeComponent();
}
....
The compiler told me I was not permitted to do this.
----------------------
Error 1
A field initializer cannot reference the nonstatic field, method, or
property...
----------------------
What have I done wrong here?
Also would you please comment on the overall decency of the first piece
of code I pasted, and if i should be using a different nameing
convention or style please let me know. As I am new to this, but trying
to learn things properly.
Thankyou all,
Gary-