Posted on Monday, August 29, 2011 9:19:27 AM and it has been read 4109 times since then.
File Is Being Used By Another Process
byte[] buffer;
using (System.IO.FileStream fileStream = new System.IO.FileStream("C:\Sample.txt",
System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
{
try
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read
// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
}
finally
{
fileStream.Close();
}
}
string result = Encoding.UTF8.GetString(buffer);
for (int i = 0; i < result.Split(Environment.NewLine.ToCharArray(),StringSplitOptions.RemoveEmptyEntries).Length; i++)
{
listbox1.Items.Insert(0, result.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[i].ToString());
}
Murat Yasar | Reply
Thursday, August 29, 2013 2:52:25 PMIf you need to use encoding, this might be helpful as well.