Blog Post by Murat Yaşar


Posted on Monday, August 29, 2011 9:19:27 AM and it has been read 1346 times since then.


File Is Being Used By Another Process

I needed a way of reading a text file which was created just before reading it at runtime.

Every time I try to read it, I was facing the same problem which was "... file is being used by another process ..."

After spending some time I came across with one keyword and it was the solution for me. "System.IO.FileShare.ReadWrite"

I try to write some sample code snippet below, hoping that it might help and/or save sometime for somebody out there.

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());
}

Have a great day.


(In order to use this feature, you have to register.)


  • Murat Yasar
    Murat Yasar | Reply
    Thursday, August 29, 2013 2:52:25 PM

    If you need to use encoding, this might be helpful as well.

    using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (StreamReader reader = new StreamReader(fs, Encoding.GetEncoding("windows-1254")))
    {
    	//Run your logic..
    }

     

Tag Related Blog Entries

TCMB Mobile Applications

Friday, January 15, 2016 1   1522  

Successfully Going Live With The New yasarmurat.com

Tuesday, May 26, 2015 0   1690  

Some Helpful Links For Software Developers

Saturday, April 28, 2012 0   3525  

Populate Nested TreeView In Asp.Net Using Common Table Expression In SQL Server

Sunday, May 01, 2011 0   3238   5

Converting String From Turkish To English

Sunday, December 19, 2010 0   649  

DataBinding GridView In Asp.NET Application By Using PostgreSQL as DataSource

Sunday, October 10, 2010 0   4170  

LINQ

Friday, October 08, 2010 0   1366  

Indicative Exchange Rates Announced by the Central Bank of Turkey For Daily Basis

Saturday, May 22, 2010 0   860   1

Debugging in .NET

Saturday, February 13, 2010 0   773  

VS 2010 / .NET 4 Release Candidate

Tuesday, February 09, 2010 0   803  

Asp.Net Application Warm Up By Using Windows Service

Thursday, February 04, 2010 6   8335   6

Tuning, Optimizing, Increasing and Improving Performance of Asp.Net Application - Part III

Saturday, January 23, 2010 0   1515  

Tuning, Optimizing, Increasing and Improving Performance of Asp.Net Application - Part II

Saturday, January 23, 2010 0   3327  

Tuning, Optimizing, Increasing and Improving Performance of Asp.Net Application - Part I

Saturday, January 23, 2010 0   2731  

Syntax not understood error for robots.txt file in Google Webmaster Tools

Tuesday, December 29, 2009 2   2467