C# 索引超出了数组systems.diagnostic.process的界限

C# 索引超出了数组systems.diagnostic.process的界限,c#,arrays,indexing,C#,Arrays,Indexing,未处理的异常: System.IndexOutOfRangeException:索引超出了数组的边界 在C:\Users\Seif中的HPLog.multi-levelpointer.ProcessMem.Main(字符串[]args)处\ Documents\Visual Studio 2010\Projects\HPLog\HPLog\Program.cs:第61行 按任意键继续 这是怎么回事:当你连一个问题都没问的时候,你会得到一个答案 System.Diagnostics.Proces

未处理的异常:

System.IndexOutOfRangeException:索引超出了数组的边界


在C:\Users\Seif中的HPLog.multi-levelpointer.ProcessMem.Main(字符串[]args)处\ Documents\Visual Studio 2010\Projects\HPLog\HPLog\Program.cs:第61行 按任意键继续


这是怎么回事:当你连一个问题都没问的时候,你会得到一个答案

System.Diagnostics.Process[] Client =  
            System.Diagnostics.Process.GetProcessesByName("Client");
 ProcessMemoryReader preader = new ProcessMemoryReader();

 if (Client != null && Client.Length > 0) {
     preader.ReadProcess = Client[0];
     preader.OpenProcess();
 }
 else {
     // Error handling...
 }

请尝试以下方法:

try
{
   System.Diagnostics.Process Client = System.Diagnostics.Process.GetProcessesByName("Client")[0];
}
catch (IndexOutOfRangeException e)
{
    System.Diagnostics.Process Client = null;
}
然后,在使用客户端之前,请确保它不为null


另外,您确定“Client”是实际的进程名称吗?

在使用数组之前,您应该测试它是否为null或包含任何项。因为您总是有可能不存在具有该名称的进程。

此外,毫无疑问:
客户端
是一个空数组-这就是为什么会出现异常。
try
{
   System.Diagnostics.Process Client = System.Diagnostics.Process.GetProcessesByName("Client")[0];
}
catch (IndexOutOfRangeException e)
{
    System.Diagnostics.Process Client = null;
}