c# IndexOf does not exist in current context

廉价感情. 提交于 2019-12-25 02:14:36

问题


I'm trying to use the following and can't wrap my head around why this IndexOf method isn't working.

foreach (string s in regKey.GetSubKeyNames())
        {
            RegistryKey sub = Registry.LocalMachine.OpenSubKey(string.Format(@"{0}\{1}", _UninstallKey64Bit, s), false);

            if (sub.ValueCount > 0)
            {
                values = sub.GetValueNames();

                if (IndexOf(values, "DisplayName")  != -1)
                {
                    string name = (sub.GetValue("DisplayName") != null) ? sub.GetValue("DisplayName").ToString() : string.Empty;

                    if (!string.IsNullOrEmpty(name) && (name.ToLower() == appName.ToLower()))
                        if (IndexOf(values, "UninstallString") != -1)
                        {
                            uninstallValue = (sub.GetValue("UninstallString") != null) ? sub.GetValue("UninstallString").ToString() : string.Empty;
                            break;
                        }
                }
            }
        }

Can anyone lend me a hand with this?


回答1:


Correct syntax is:

if (Array.IndexOf(values, "DisplayName")  != -1)



回答2:


GetValueNames() returns a string array so you probably are looking for:

if (values.Contains("DisplayName"))



回答3:


Try

 if (values.Any(v => v == "DisplayName")) )
 {
    ...
 }



回答4:


Try changing -

if (IndexOf(values, "UninstallString") != -1)

to

if (Array.IndexOf(values, "UninstallString") != -1)



回答5:


Try this instead of just IndexOf.

Array.IndexOf(values, "DisplayName")


来源:https://stackoverflow.com/questions/7423114/c-sharp-indexof-does-not-exist-in-current-context

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!