More questions on finding subdirectories in C #

I recently asked a question about finding directories in C # .

Program logic

  • List all directories in a line or several lines.
  • Try copying the file using the line.
    • If this fails, continue to the next line,
  • If you can find the file:
    • Copy
    • go to next record in text file

Final result

If he finds it on the 4th line, he will not try the next 15 lines.

Code snippet

 if (checkBox2.Checked)
        {
            string[] file_names = File.ReadAllLines(@"c:\dact.txt");
            string path1 = @"I:\pa\test\";
            string path2 = @"I:\pa\test2\";
            string path3 = @"I:\pa\test3\";
            string path4 = @"I:\pa\test4\";

            string full = (string.Concat(path1, file_names, ".txt"));
            foreach (string file_name in file_names)
            if (System.IO.File.Exists(full))
            {
                foreach (string file in file_names)
                System.IO.File.Copy(file, 
                                    @"C:\" + 
                                    textBox1.Text + 
                                    @"\\OC\" + 
                                    file_name + ".txt");
            }
        }

The above code does not contain the ability to write files with an error, I'm not sure how to do this.

, : 1.
2.
3. , ,
4. " ", .
5. - 19- .

0
2

:

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);
    }
}

: ...

0

, .

Dictionaty, List > , - , - .

...

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);
                }
            }
        }
0

All Articles