When you read a file byte by byte, it can take too long. I would recommend that you read a block of bytes (for example 1024 or 2048) in the loop. Then, in the block you have read, use a regular expression to match your character, particularly if you have a extremely large file.
Sample code would be something like this:
private string GetFileData(string fileName, string matchChar)
{
StringBuilder x = new StringBuilder();
int blockCount = 2048;
int offset = 0;
string pattern = matchChar;
int k = -1;
using (var sr = new System.IO.FileStream(fileName, System.IO.FileMode.Open))
{
while ((sr.CanRead) && (k != 0))
{
byte[] bt = new byte[blockCount];
k = sr.Read(bt, 0, blockCount);
string so = System.Text.UTF8Encoding.UTF8.GetString(bt);
var m = new System.Text.RegularExpressions.Regex(pattern).Matches(so);
foreach (System.Text.RegularExpressions.Match item in m)
{
x.Append(item.Value);
}
}
}
return x.ToString();
}
You would call this as
GetFileData(@"c:\matchtest.ono", "a");