-2

I try to read the data of all files through a loop, but there is a problem with starting the program ?

Meteorological reference

        string filePath = @"C:\Meteo\*.dat";

        List<string> lines =  File.ReadAllLines(filePath).ToList();

        foreach (string line in lines)
        {
            MessageBox.Show(line);
        }

The output is:

************** Exception Text **************
System.ArgumentException: Illegal characters in path.
   at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
   at System.IO.StreamReader..ctor(String path, Encoding encoding)
   at System.IO.File.InternalReadAllLines(String path, Encoding encoding)
   at System.IO.File.ReadAllLines(String path)
   at WindowsFormsApp1.frm_main.frm_main_Load(Object sender, EventArgs e) in C:\Users\admin\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1\frm_main.cs:line 103
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

The files look like this:.dat file

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Ben Johnson
  • 753
  • 1
  • 9
  • 18
  • 1
    You want to first get your file names. Start with Directory.GetFiles and put in your base path and search term. Then you can loop through the files and read their contents.This question can help get you started: https://stackoverflow.com/questions/6604885/how-can-i-perform-full-recursive-directory-file-scan/6604915#6604915 – Anthony Pegram May 30 '19 at 07:05

1 Answers1

1

You should enumerate files:

var files = Directory.EnumerateFiles(@"C:\Meteo", "*.dat");

Then you can use SelectMany if you want to glue the files' lines together:

// all lines of all *.dat files in the single list
List<string> lines = Directory
  .EnumerateFiles(@"C:\Meteo", "*.dat")
  .SelectMany(file => File.ReadLines(file))
  .ToList();

Or you can process each file in a loop:

foreach (string file in Directory.EnumerateFiles(@"C:\Meteo", "*.dat")) {
  //TODO: process the file here
}

Edit: Let's process each file in a loop (see comments below)

// for each file
foreach (string file in Directory.EnumerateFiles(@"C:\Meteo", "*.dat")) {
  // read each line
  foreach (string line in File.ReadLines(file)) {
    // and show file name and line in a message box
    MessageBox.Show(line, file);
  }
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • I want to see the data on message box, but in the foreach loop when I write MessageBox.Show(file) I see only names on the files in this folder. I want to get a every value in this files ? – Ben Johnson May 30 '19 at 08:11
  • @Ben Johnson: if you want to have a look at files' contents you have to read files with a help of `File.ReadLines(file)`. Please, see my edit – Dmitry Bychenko May 30 '19 at 08:17