如何检索多次选择的ListView C#的不同项目

How to retrieve multiple times selected different item of ListView C#

我需要在 WinForm C# 中制作一个应用程序作为我的最终编程项目。该项目是关于更好地管理注册表,使用户更容易编辑值。

所以我需要 ListView 中的 注册表路径 来检查值 UninstallString 是否存在。如您所见,标签 Current uninstall option is: 根据 selected 项目更新(这里是 Adobe Creative Cloud) .问题是当我再次按下 select 其他程序时它崩溃了:

private void listInstalled_SelectedIndexChanged(object sender, EventArgs e)
    { 
        uninstall_optn.Enabled = listInstalled.Items.Count > 0;
        string retrieveAppPath = listInstalled.SelectedItems[0].SubItems[2].Text; //Error happens here: System.ArgumentOutOfRangeException: 'InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index'
        retrieveAppPath = retrieveAppPath.Remove(0, 19);
        RegistryKey selectedAppPath = Registry.LocalMachine.OpenSubKey(retrieveAppPath, true);
        if (ValueExists(selectedAppPath, "UninstallString"))
        {
            uninstall_crnt.Text = "Current uninstall option is: Enabled";
        }
        else
        {
            uninstall_crnt.Text = "Current uninstall option is: Disabled";
        }
    }

(我评论了发生错误的行和错误)。

您尝试使用此行仅访问一项

string retrieveAppPath = listInstalled.SelectedItems[0].SubItems[2].Text;

如果您想获取所有选定项目的列表,试试这个

private bool _isUninstalEnabled;
        private List<string> _myUninstallList; //list contains all selected item path in your case 

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        _myUninstallList = new List<string>();
        _isUninstalEnabled = listView1.Items.Count > 0;
        for (int i = 0; i < listView1.SelectedIndices.Count; i++)
        {
            string selectItemValue = listView1.Items[listView1.SelectedIndices[i]].SubItems[2].Text; //Registry path in your case
            _myUninstallList.Add(selectItemValue);

        }
    }