Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am making an app and it needs to open at least 676 files simultaneously to a maximum of 1400 files. I will be writing to these files using the StreamWriter Class and reading the data using StreamReader Class. So, is there a maximum limit on the no of files that can be opened simultaneously for reading or writing in C# just like VC++ as described in the following link. Is there a limit on number of open files in Windows.

like image 854
Pratik Singhal Avatar asked Nov 29 '13 16:11

Pratik Singhal


People also ask

What is an open file limit?

The open-file limit is a setting that controls the maximum number of open files for individual users (such as non-root users). The default open-file limit is typically 1024.

What does Too many open files mean?

The "Too many open files" message means that the operating system has reached the maximum "open files" limit and will not allow SecureTransport, or any other running applications to open any more files. The open file limit can be viewed with the ulimit command: The ulimit -aS command displays the current limit.

How do you increase number of open files limit in Linux?

You can increase the maximum number of open files on the Linux host by setting a new value in the kernel variable file, /proc/sys/fs/file-max. This command forces the limit to 262144 files which is four times the default setting. (The default setting is appropriate for many environments.)


2 Answers

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

like image 100
bhouston Avatar answered Nov 15 '22 00:11

bhouston


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();
  }
like image 29
Ondrej Svejdar Avatar answered Nov 15 '22 00:11

Ondrej Svejdar