如何检查如果列表中已经存在的文件吗? [英] How can i check if a files already exist in the List?

查看:104
本文介绍了如何检查如果列表中已经存在的文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

for (int i = 0; i < files.Count; i++)
{
    if (pdf1.Lightnings.Count == 0)
    {
        pdf1.Lightnings.Add(files[i]);
    }
    if (files[i] != pdf1.Lightnings[i])
    {
        pdf1.Lightnings.Add(files[i]);
    }
}



这两个文件,闪电是清单<串>

例如在文件中我有33指数(文件名),我想将它们添加到闪电列表。
,而我要检查,如果从文件列表中的文件名中的闪电已经存在,所以不要再添加它。

For example in files I have 33 indexes (files names) and I want to add them to the Lightnings List. But I want to check that if the file name from the List of files already exist in Lightnings so don't add it again.

事情是这样的现在我得到错误,因为当变量i = 1,因此该行:

The way it is now I'm getting error since when the variable i=1 so the line:

if (files[i] != pdf1.Lightnings[i])

由于闪电我有抛出一个错误只有一个索引[0],在I = 1已

Throws an error since in Lightnings i have only one index [0] and in i=1 already

推荐答案

您可以使用的 。载 方式:

if (!pdf1.Lightnings.Contains(files[i]))
    pdf1.Lightnings.Add(files[i]);

这将检查文件[I] 尚不集合中添加之前就已存在。

This will check that files[i] does not already exist in the collection before adding.

这篇关于如何检查如果列表中已经存在的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆