2016-12-06 136 views
-2

我正在一个ID测试仪。我每秒都会收到错误CS1061。 这是我的代码:我不知道为什么我收到错误CS1061

Process[]roblox=Process.GetProcessesByName("RobloxPlayerBeta.exe"); 
Console.WriteLine("Id of RobloxPlayerBeta.exe is:", roblox.Id); 
int id = roblox.Id; 
+5

我知道你为什么会得到它,但我非常认真地写回答 –

+1

显然,分享整个错误信息 –

+2

@AlfieGoodacre很好的答案! –

回答

2

编译器错误CS1061会给你,为什么你得到这个错误的详细信息。但在你的问题中,你没有包含错误细节的最重要部分。 roblox是数组,如果您需要获取进程的id,那么您需要获取数组中的项目。阵列的项目可以通过指数

Process[]roblox=Process.GetProcessesByName("RobloxPlayerBeta.exe"); 

if(roblox!=null && roblox.Length >0) 
{ 
    Console.WriteLine("Id of RobloxPlayerBeta.exe is:", roblox[0].Id); 
    int id = roblox[0].Id; 
} 
+0

谢谢!这工作很好! – ForkerGup

2

你的过程roblox一个数组访问,但尝试访问的整个事情的Id财产,而不是好...的过程。为了解决这个问题,你必须真正选择索引您希望使用的

Process[] roblox = Process.GetProcessesByName("RobloxPlayerBeta.exe"); 
if(roblox.GetLength(0) > 0) //Check that any processes exist with that name 
{ 
    int id = roblox[0].Id; //set ID first as to avoid accessing the array twice 
    Console.WriteLine("Id of RobloxPlayerBeta.exe is:" + id); //Write the line using the newly found id variable 
} 
else //If the process doesn't exist 
{ 
    Console.WriteLine("RobloxPlayerBeta.exe is not running"); //Output error of sorts 
} 

这里还有固定的Id是你Console.WriteLine(),你使用它需要这样Console.WriteLine("Id of RobloxPlayerBeta.exe is:{0}", id);或使用级联像一个参数的方式我在这个例子中做了。你似乎已经尝试了两下,如果您有获取与阵列交手的任何问题不工作

的混合物,有一个读of this article

套用它,就你有问题一旦你有一个数组,例如

string[] stringArray = new string[] {"hello", "hi"}; 

您可以访问像这样

string firstIndex = stringArray[0]; //for the first index 
string secondIndex = stringArray[1]; //for the second index 

其包含的对象。如果我们再编写这

Console.WriteLine(firstIndex + secondIndex); 

这将输出hellohi

作为一个侧面说明,您收到错误CS1061特别是因为你试图访问一个不存在

NB阵列的性能:一个有趣的单线程,使用-1来表明过程未运行

int id = Process.GetProcessesByName("RobloxPlayerBeta.exe")?[0].Id ?? -1; 
+0

谢谢!这工作很好!很好解释! – ForkerGup

+0

@ForkerGup此外,如果您希望直接访问属性,而不是'Process [] roblox = Process.GetProcessesByName(“RobloxPlayerBeta.exe”);'',您可以执行'Process roblox = Process。 GetProcessesByName( “RobloxPlayerBeta.exe”)[0];'。此代码会将进程设置为roblox进程,或者如果该进程未运行,则会将其设置为空 –

相关问题