0

I have write a c# code to retrieve all the file names follow by printing one of the information inside. Example File contains (file1.mht, file2.mht, file3.mht). Maybe the contain inside is (aaaaaa, bbbbbb, cccccc) follow the sequence of the file. Example out output: file1.mht aaaaaa file2.mht bbbbbb file3.mht cccccc

But I encounter the problem it cannot loop the file name follow by showing the content inside. Anyone can helps? Current result is it show all the directory first and only done the work for the first one in directory.

using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Specialized;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo mht_file = new DirectoryInfo(@"C:\Users\liewm\Desktop\SampleTest\");
            FileInfo[] Files = mht_file.GetFiles("*.mht");
            string str = "";
            string mht_text = "";
            string directory = "";
            string listInfo = "";

            foreach (FileInfo file in Files)
            {
                str = file.Name;
                directory = mht_file + str;
                Console.WriteLine(directory);
            }
            foreach (char filePath in directory)
            {
               //Here is my work to retrieve the data in the file

                Console.WriteLine("Names:" + str + "    " + "Component:" + component);
                Console.ReadKey();
            }


        }


    }

}
tester
  • 3
  • 4
  • 1
    In `foreach (char filePath in directory)`filepath is not the path to a file but a letter from the directory string – xdtTransform Mar 07 '19 at 12:27
  • Possible duplicate of [how to read all files inside particular folder](https://stackoverflow.com/questions/5840443/how-to-read-all-files-inside-particular-folder) – xdtTransform Mar 07 '19 at 12:29
  • @xdtTransform actually all my file path store in the directory variable in previous step. – tester Mar 07 '19 at 12:35
  • Actually.. No! `char filePath` is a [char](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/char). One Unicode character. And `string directory = "";`, directory is a string. Meaning that the for each loop on each Unicode character from the string variable name directory. – xdtTransform Mar 07 '19 at 12:55
  • And iterate throught a dictionary finding all file with and extention and display the content of those file is exactly the accepted answer in the duplicate. – xdtTransform Mar 07 '19 at 13:38

3 Answers3

1

From your question I understand that you want to print the file name followed by the content of the same, if so you can try:

DirectoryInfo mht_file = new DirectoryInfo(@"C:\Users\liewm\Desktop\SampleTest\");
FileInfo[] Files = mht_file.GetFiles("*.mht");

foreach (FileInfo file in Files)
{
    // read the content of the file
    var content = File.ReadAllText(file.FullName);

    // from your question "Example out output: file1.mht aaaaaa"
    Console.WriteLine($"{file.Name} {content}");
}
Alessandro D'Andria
  • 8,663
  • 2
  • 36
  • 32
0

only done the work for the last one in directory.

Yes, that's cause your directory variable is a string string directory = "" which will get overridden by the last value of loop iteration. You rather want to store in a string[] rather if you want to process all of them.

foreach (FileInfo file in Files)
{
  str = file.Name;
  directory = mht_file + str;
  Console.WriteLine(directory);
}
Rahul
  • 76,197
  • 13
  • 71
  • 125
0

Please try this.

  foreach (FileInfo file in Files)
            {
                str = file.Name;
                directory += mht_file + str;
                Console.WriteLine(directory);
            }
Hemang A
  • 1,012
  • 1
  • 5
  • 16