Is there a limit on the maximum number of files that can be opened in C #?

I am creating an application and it needs to open at least 676 files at the same time up to 1400 files. I will write these files using the class StreamWriterand read the data using the StreamReaderClass. So, is there a maximum restriction on the lack of files that can be opened at the same time for reading or writing in C #, like VC ++, as described in the following link. Is there a limit on the number of open files in Windows .

+4
source share
2 answers

The upper limit for files opened by .NET is determined by the limit imposed on the Win32 CreateFile API, which is 16384.

+5
source

This works for me:

  var streams = new Stream[10000];
  for (var i = 0; i < streams.Length; i++) {
    streams[i] = File.OpenWrite(Path.Combine(Path.GetTempFileName()));
    streams[i].WriteByte((byte)'A');
  }
  var tasks = new Task[streams.Length];
  for (var i = 0; i < streams.Length; i++) {
    var index = i;
    tasks[i] = new Task(() => {
      streams[index].WriteByte((byte)'B');
    });
    tasks[i].Start();
  }
  Task.WaitAll(tasks);
  for (var i = 0; i < streams.Length; i++) {
    streams[i].Close();
  }
+6
source

All Articles