C#在listview中重复

C#在listview中重复,c#,C#,所以我一直在写一个程序来帮助我监控工作中的一些事情,我让他们写入一个.txt文件,但是当我监控的目录中存在一个文件时,它会继续将文件写入这个txt文件,例如exmaple New Text Document.txt - POA Submitted Directory - Current Time: 11:40:15 AM 1/9/2017 | Time Received: 1/9/2017 11:21:16 AM New Text Document.txt - POA Directory - C

所以我一直在写一个程序来帮助我监控工作中的一些事情,我让他们写入一个.txt文件,但是当我监控的目录中存在一个文件时,它会继续将文件写入这个txt文件,例如exmaple

New Text Document.txt - POA Submitted Directory - Current Time: 11:40:15 AM 1/9/2017 | Time Received: 1/9/2017 11:21:16 AM
New Text Document.txt - POA Directory - Current Time: 11:40:25 AM 1/9/2017 | Time Received: 1/9/2017 11:21:16 AM
New Text Document.txt - MTM Directory - Current Time: 12:48:28 PM 1/9/2017 | Time Received: 1/9/2017 11:21:16 AM
New Text Document.txt - MTM Directory - Current Time: 12:49:08 PM 1/9/2017 | Time Received: 1/9/2017 11:21:16 AM
New Text Document.txt - MTM Directory - Current Time: 12:49:18 PM 1/9/2017 | Time Received: 1/9/2017 11:21:16 AM
我应该如何避免重复

下面是我获取目录列表的方法

private void POACheck(object sender, EventArgs e)
{
    listView2.Items.Clear();
    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("Z:/FTPRoot/PDCUpload/POA");
    foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
    {
        FlashWindow(this.Handle, true);
        ListViewItem lSingleItem = listView2.Items.Add(f.Name);
        using (System.IO.StreamWriter file =
        new System.IO.StreamWriter(@"C:\Users\sisson.chad\Desktop\POA-MTM.txt", true))
        {
            file.WriteLine(f.Name + " - POA Directory - Current Time: " + DateTime.Now.ToString("h:mm:ss tt") + " " + DateTime.Now.ToString("M/d/yyy") + " | Time Received: " + f.LastWriteTime);
        }

    }
}
感谢大家的帮助,对不起,我是C#的新手,我还在学习

欢迎来到C#。此外,要
列表

private void POACheck(对象发送方,事件参数e)
{
listView2.Items.Clear();
System.IO.DirectoryInfo dir=new System.IO.DirectoryInfo(“Z:/FTPRoot/PDCUpload/POA”);
变量名称=新列表();
foreach(dir.GetFiles(“**”)中的System.IO.FileInfo f)
{
if(Names.IndexOf(f.Name)==-1)/-1来自IndexOf的结果表示它不存在。
{
FlashWindow(this.Handle,true);
ListViewItem lSingleItem=listView2.Items.Add(f.Name);
使用(System.IO.StreamWriter)文件=
new System.IO.StreamWriter(@“C:\Users\sisson.chad\Desktop\POA-MTM.txt”,true))
{
file.WriteLine(f.Name+“-POA目录-当前时间:”+DateTime.Now.ToString(“h:mm:ss tt”)+“+DateTime.Now.ToString(“M/d/yyy”)+“|接收时间:”+f.LastWriteTime);
名称。添加(f.名称);
}
}
}
}

Opps,对不起,我的标签现在好了吗?一种方法是维护一个已经写入元素的列表,并且只写入新的元素。请阅读并接受StreamWriter参数“true”正在追加,而不是创建新文件。@jdweng我需要它写入同一个.txt文件,我需要它不写入同一行5次Chad,您是否在事件外部测试了文件列表片段(事件的核心禁止ListView和FlashWindow调用)并正常工作,对的我会尝试使用异步等待技术来实现此事件,让它返回一个任务并找出代码段的哪个部分需要等待。New Text Document.txt-POA目录-当前时间:2017年1月9日下午2:12:08 |接收时间:2017年1月9日11:21:16 AM New Text Document.txt-POA目录-当前时间:2017年1月9日下午2:12:18 |接收时间:2017年1月9日11:21:16 AM New Text Document.txt-POA目录-当前时间:2017年1月9日下午2:12:28 |收到时间:2017年1月9日上午11:21:16此文件仍在编写,感谢您的尝试!
      private void POACheck(object sender, EventArgs e)
        {
            listView2.Items.Clear();
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("Z:/FTPRoot/PDCUpload/POA");            
            var Names = new List<string>();
            foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
            {
               if(Names.IndexOf(f.Name) == -1) //-1 result from IndexOf means it's not there.
                {
                  FlashWindow(this.Handle, true);
                  ListViewItem lSingleItem = listView2.Items.Add(f.Name);
                  using (System.IO.StreamWriter file =
                  new System.IO.StreamWriter(@"C:\Users\sisson.chad\Desktop\POA-MTM.txt", true))
                  {

                       file.WriteLine(f.Name + " - POA Directory - Current Time: " + DateTime.Now.ToString("h:mm:ss tt") + " " + DateTime.Now.ToString("M/d/yyy") + " | Time Received: " + f.LastWriteTime);
                       Names.Add(f.Name);
                  }
                }

            }
        }