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.
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.
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.
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.)
The upper limit on files opened by .NET is governed by the limit imposed on the Win32 API CreateFile, which is 16384.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With