我有一个函数可以每隔10秒获取正在运行的应用程序,将它们放在列表框中,如果单击“发送”按钮,则将它们发送到另一个窗口。现在的问题是,每当我尝试打开然后立即关闭应用程序时,它会发送指向我的列表的错误。 我不知道该怎么做。
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.
这是我的代码,如果它带来任何帮助:
private List<int> listedProcesses = new List<int>();
private void SendData()
{
String processID = "";
String processName = "";
String processFileName = "";
String processPath = "";
string hostName = System.Net.Dns.GetHostName();
listBox1.BeginUpdate();
try
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
piis = GetAllProcessInfos();
try
{
if (!listedProcesses.Contains(piis[i].Id)) //place this on a list to avoid redundancy
{
listedProcesses.Add(piis[i].Id);
processID = piis[i].Id.ToString();
processName = piis[i].Name.ToString();
processFileName = piis[i].FileName.ToString();
processPath = piis[i].Path.ToString();
output.Text += "\n\nSENT DATA : \n\t" + processID + "\n\t" + processName + "\n\t" + processFileName + "\n\t" + processPath + "\n";
}
}
catch (Exception ex)
{
wait.Abort();
output.Text += "Error..... " + ex.StackTrace;
}
NetworkStream ns = tcpclnt.GetStream();
String data = "";
data = "--++" + " " + processID + " " + processPath + " " + processFileName + " " + hostName;
if (ns.CanWrite)
{
byte[] bf = new ASCIIEncoding().GetBytes(data);
ns.Write(bf, 0, bf.Length);
ns.Flush();
}
}
}
finally
{
listBox1.EndUpdate();
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ProcessInfoItem pii = piis.FirstOrDefault(x => x.Id == (int)(sender as ListBox).SelectedValue); //setting value for list box
if (pii != null)
{
string hostName = System.Net.Dns.GetHostName();
textBox4.Text = listBox1.SelectedValue.ToString();
textBox5.Text = (pii.FileName);
textBox6.Text = (pii.Path);
textBox7.Text = hostName;
}
}
private List<ProcessInfoItem> piis = new List<ProcessInfoItem>();
private void Form1_Load(object sender, EventArgs e)
{
piis = GetAllProcessInfos();
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Id";
listBox1.DataSource = piis;
textBox1.Text = GetIpAdd().ToString();
}
private List<ProcessInfoItem> GetAllProcessInfos()
{
List<ProcessInfoItem> result = new List<ProcessInfoItem>();
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (!String.IsNullOrEmpty(p.MainWindowTitle))
{
ProcessInfoItem pii = new ProcessInfoItem(p.Id,p.MainModule.ModuleName, p.MainWindowTitle, p.MainModule.FileName);
result.Add(pii);
}
}
return result;
}
public class ProcessInfoItem
{
public int Id { get; set; }
public string Name { get; set; }
public string FileName { get; set; }
public string Path { get; set; }
public ProcessInfoItem(int id, string name, string filename, string path)
{
this.Id = id;
this.Name = name;
this.FileName = filename;
this.Path = path;
}
}
答案 0 :(得分:4)
您正在索引for
循环引用的其他集合。听起来你可能想要:
piis = GetAllProcessInfos();
for (int i = 0; i < piis.Count; i++)
{
代替。但是,您在 for
循环中调用函数形式,因此不清楚应该迭代什么。
答案 1 :(得分:0)
尝试改变,
for (int i = 0; i < listBox1.Items.Count; i++)
{
piis = GetAllProcessInfos();
到
piis = GetAllProcessInfos();
for (int i = 0; i < piis.Count; i++)
{