Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read multiple files from server into c#

Tags:

c#

file

I want to know how to read multiple (about 500-1000) text files which are located on a server. So far, I've written code for a program that only reads a single text file.

Here's how I'm currently reading a single file.

  public void button1_Click(object sender, EventArgs e)
    {
        // Reading/Inputing column values



        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {

            string[] fileLines = File.ReadAllLines(ofd.FileName);

I would like to get rid of the open file dialog box, and let the program automatically read the 500-1000 text files where are located in the sever.

I'm thinking something along the lines of

  for (int i =0; i<numFiles; i++)
  {
     //just use string[] fileLines =File.ReadAllLines()
     //how would i specify the path for multiple files?
  }

Questions are then:

  1. How would I approach this?
  2. How exactly should I get the number of files?
    (I'm guessing I'd have to read the server file which contains them.)
like image 971
Newuser Avatar asked Oct 03 '22 12:10

Newuser


2 Answers

You can use Directory.GetFiles(string path) to get all files from a certain directory. You can then use a foreach loop to iterate through all the files in that directory and do your processing.

like image 139
Bluetarsky Avatar answered Oct 08 '22 06:10

Bluetarsky


You can use recursion to loop through all directories. Using Directory.EnumerateFiles also allows you to use a foreach loop so you don't have to worry about the file count.

    private static void ReadAllFilesStartingFromDirectory(string topLevelDirectory)
    {
        const string searchPattern = "*.txt";
        var subDirectories = Directory.EnumerateDirectories(topLevelDirectory);
        var filesInDirectory = Directory.EnumerateFiles(topLevelDirectory, searchPattern);

        foreach (var subDirectory in subDirectories)
        {
            ReadAllFilesStartingFromDirectory(subDirectory);//recursion
        }

        IterateFiles(filesInDirectory, topLevelDirectory);
    }

    private static void IterateFiles(IEnumerable<string> files, string directory)
    {
        foreach (var file in files)
        {
            Console.WriteLine("{0}", Path.Combine(directory, file));//for verification
            try
            {
                string[] lines = File.ReadAllLines(file);
                foreach (var line in lines)
                {
                    //Console.WriteLine(line);   
                }
            }
            catch (IOException ex)
            {
                //Handle File may be in use...                    
            }
        }
    }

Also note Directory.EnumerateFiles provides overload that lets you provide a search pattern to narrow the scope of the files.

like image 42
P.Brian.Mackey Avatar answered Oct 08 '22 06:10

P.Brian.Mackey