C# 关于在C中搜索子目录的进一步问题#

C# 关于在C中搜索子目录的进一步问题#,c#,.net,file-io,C#,.net,File Io,最近我问了一个问题 程序逻辑 在一个字符串或多个字符串中列出所有目录 尝试使用字符串将文件复制出去。 如果失败,请转至下一个字符串 如果成功找到该文件: 抄袭 跳到文本文件中的下一项 最终结果 如果它在第4个字符串中找到它,则不会尝试接下来的15个字符串 代码片段 上面的代码不包含写出失败文件的功能,我不知道该怎么做 这就是我想要的: 1.读取文本文件的所有行 2.尝试从特定目录复制文件 3.无论什么文件失败,它都会将失败的文件写入文本文件 4.它读取所有新的“失败列表”,然后尝试我

最近我问了一个问题

程序逻辑
  • 在一个字符串或多个字符串中列出所有目录
  • 尝试使用字符串将文件复制出去。
    • 如果失败,请转至下一个字符串
  • 如果成功找到该文件:
    • 抄袭
    • 跳到文本文件中的下一项
最终结果 如果它在第4个字符串中找到它,则不会尝试接下来的15个字符串

代码片段 上面的代码不包含写出失败文件的功能,我不知道该怎么做

这就是我想要的: 1.读取文本文件的所有行
2.尝试从特定目录复制文件
3.无论什么文件失败,它都会将失败的文件写入文本文件
4.它读取所有新的“失败列表”,然后尝试我列表中的第二条路径。

5.将过程重复到第三条路径-第十九条路径

这是我的第一个想法:

String[] filesToCopy = File.ReadAllLines("yourFileHere.txt");

String sourceFolder = "yourSourceFolderHere";
String destinationFolder = "yourDestinationFolderHere";

foreach (String subFolder in Directory.GetDirectories(sourceFolder))
{
    for (int i = 0; i < filesToCopy.Length; i++)
    {
        if (!String.IsNullOrEmpty(filesToCopy[i]))
        {
        if (File.Exists(Path.Combine(subFolder, filesToCopy[i])))
        {
            File.Copy(Path.Combine(subFolder, filesToCopy[i]), Path.Combine(destinationFolder, FilesToCopy[i]));
            filesToCopy[i] = string.Empty;
        }
        }
    }
}

using (StreamWriter strm = new StreamWriter("yourOutputFile.txt"))
{
    foreach (String fileToCopy in filesToCopy)
    {
        if (!String.IsNullOrEmpty(fileToCopy))
        strm.WriteLine(fileToCopy);
    }
}
String[]filesToCopy=File.ReadAllLines(“yourFileHere.txt”);
String sourceFolder=“yourSourceFolderHere”;
String destinationFolder=“yourdstinationfolderhere”;
foreach(Directory.GetDirectories(sourceFolder))中的字符串子文件夹)
{
for(int i=0;i

编辑:修复了错误的文件复制…

这是我为查找特定类型的文件而创建的一个简单类

它将结果存储在字典列表>中,其中第一个字符串是目录的完整路径名,列表是每个文件的完整路径

之后,您只需将文件复制并粘贴到其他位置

public class CheckForFile
    {
        private Dictionary<String, List<String>> FileDirectoryList;
        private String StartLocation;
        private String Format;

        public CheckForFile(String StartLocation, String Format)
        {
            this.FileDirectoryList = new Dictionary<string, List<string>>();
            this.StartLocation = StartLocation;
            this.Format = Format;
        }

        public void Discover()
        {
            DirectoryInfo di = new DirectoryInfo(this.StartLocation);
            DirectoryInfo[] ChildDirectoryList = di.GetDirectories("*", SearchOption.AllDirectories);

            foreach(DirectoryInfo ChildDir in ChildDirectoryList)
            {
                Console.WriteLine(ChildDir.FullName);
                FileInfo[] FileInfoList = ChildDir.GetFiles(this.Format);

                this.FileDirectoryList.Add(ChildDir.FullName, new List<string>());

                foreach (FileInfo ChildFileInfo in FileInfoList)
                {
                    Console.WriteLine("\t\t" + ChildFileInfo.Name);
                    this.FileDirectoryList[ChildDir.FullName].Add(ChildFileInfo.FullName);
                }
            }
        }
公共类CheckForFile
{
私有字典文件目录列表;
私密字符串定位;
私有字符串格式;
public CheckForFile(字符串格式)
{
this.FileDirectoryList=新字典();
this.startolocation=startolocation;
this.Format=Format;
}
公共空间发现()
{
DirectoryInfo di=新的DirectoryInfo(this.asp);
DirectoryInfo[]ChildDirectoryList=di.GetDirectory(“*”,SearchOption.AllDirectory);
foreach(ChildDirectoryList中的DirectoryInfo ChildDir)
{
Console.WriteLine(ChildDir.FullName);
FileInfo[]FileInfoList=ChildDir.GetFiles(this.Format);
Add(ChildDir.FullName,new List());
foreach(FileInfo列表中的FileInfo ChildFileInfo)
{
Console.WriteLine(“\t\t”+ChildFileInfo.Name);
this.FileDirectoryList[ChildDir.FullName].Add(ChildFileInfo.FullName);
}
}
}

什么是“字符串中的所有目录”?此字符串来自何处?它将手动创建,无法提取子目录,因为它将导致结构中的文件和文件夹出现内存不足问题,因此基本上字符串路径1=@“I:\OC\path1”我很困惑。你到底想做什么?你正在概述一些算法来做什么?检查编辑,我会尝试让这个请求更清楚一点…它不喜欢File.Copy(Path.Combine(subFolder,filesToCopy[I]),destinationFolder);持续输出找不到路径的一部分。是否有人能帮我处理上述代码,它不会复制File.copy(Path.Combine(子文件夹,filesToCopy[i]),destinationFolder)处的文件;我更改为此文件。copy(子文件夹+“\\”+filesToCopy[1],destinationFolder+filesToCopy[1]);而且它一直写着相同的文件名呃,我应该让它简单点!谢谢Henknow快速提问,我如何将“.txt”添加到文件列表文件中,这意味着列表中的所有文件都丢失了.txt,那么我如何将其添加到搜索功能中
public class CheckForFile
    {
        private Dictionary<String, List<String>> FileDirectoryList;
        private String StartLocation;
        private String Format;

        public CheckForFile(String StartLocation, String Format)
        {
            this.FileDirectoryList = new Dictionary<string, List<string>>();
            this.StartLocation = StartLocation;
            this.Format = Format;
        }

        public void Discover()
        {
            DirectoryInfo di = new DirectoryInfo(this.StartLocation);
            DirectoryInfo[] ChildDirectoryList = di.GetDirectories("*", SearchOption.AllDirectories);

            foreach(DirectoryInfo ChildDir in ChildDirectoryList)
            {
                Console.WriteLine(ChildDir.FullName);
                FileInfo[] FileInfoList = ChildDir.GetFiles(this.Format);

                this.FileDirectoryList.Add(ChildDir.FullName, new List<string>());

                foreach (FileInfo ChildFileInfo in FileInfoList)
                {
                    Console.WriteLine("\t\t" + ChildFileInfo.Name);
                    this.FileDirectoryList[ChildDir.FullName].Add(ChildFileInfo.FullName);
                }
            }
        }