C# 为什么复制到特定文件夹要慢得多?

C# 为什么复制到特定文件夹要慢得多?,c#,system.io.file,system.io.directory,C#,System.io.file,System.io.directory,我在将文件从一个文件夹复制到同一驱动器上的另一个文件夹时遇到了一个奇怪的问题 如果我复制到一个特定的文件夹问题文件夹复制需要很长时间 如果我复制到我创建的另一个测试文件夹OK_文件夹,复制速度非常快 我第一次注意到这个问题是在我不小心完全填满了我的C驱动器,并且不得不从problem\u文件夹中删除大量文件之后 这里可能有什么问题 using System; using System.Collections.Generic; using System.ComponentModel; using

我在将文件从一个文件夹复制到同一驱动器上的另一个文件夹时遇到了一个奇怪的问题

如果我复制到一个特定的文件夹问题文件夹复制需要很长时间

如果我复制到我创建的另一个测试文件夹OK_文件夹,复制速度非常快

我第一次注意到这个问题是在我不小心完全填满了我的C驱动器,并且不得不从problem\u文件夹中删除大量文件之后

这里可能有什么问题

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace TestCopy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

           MoveFiles(@"C:\HOTDATA\DB\TEST", @"C:\PROBLEM_FOLDER\DB");
           //MoveFiles(@"C:\HOTDATA\DB\TEST", @"C:\OK_FOLDER\DB");
        }

        public void MoveFiles(string sourceDir, string targetDir)
        {
            foreach(var file in Directory.GetFiles(sourceDir,"nEq*-1m*"))
            {

                string FileName = Path.GetFileName(file);

                var targetPath = Path.Combine(targetDir, FileName);
                if (File.Exists(targetPath))
                {
                    //File.Delete(targetPath);
                }
                else
                {
                    File.Move(file, targetPath);
                }
            }
        }

    }
}

您可能在该文件夹中隐藏了系统文件。更改视图设置以显示隐藏并取消选中隐藏受保护的系统文件。在过去,c#是出了名的缓慢迭代文件夹,你必须以某种方式处理大型文件夹。这是几年前的事了,所以我不知道现在是否仍然是这样。

我不知道NTFS(你很可能有)是如何工作的,但我想它和FAT处理满是文件的文件夹有同样的问题。文件夹中的文件越多=速度越慢。证明了这一点。试着用CCleaner清理你的注册表。你的
问题\u文件夹中有更多文件吗?如果是这样,则此代码
文件.Exists(targetPath)
将变慢。你有任何文件夹的索引吗?@christiandev。我可以把文件拿出来。我还是一样problem@Glubus. 为什么你认为登记册可能是个问题?仅用于特定文件夹?