Blog Post by Murat Yaşar


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

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

Creating Connection and Making CRUD Operations From Your .NET Project to Oracle DB

Thursday, December 29, 2022 0   2535  

Scratching Beneath the Surface

Friday, March 11, 2022 0   2555  

Calculate Time Difference Between Two Dates Excluding Weekend and Special Holidays

Monday, March 07, 2022 0   2091  

API For Getting Indicative Exchange Rates From Central Bank of the Republic of Turkey

Saturday, September 01, 2018 1   3543   1

TCMB Mobile Applications

Friday, January 15, 2016 1   4959  

Successfully Going Live With The New yasarmurat.com

Tuesday, May 26, 2015 0   3707  

Some Helpful Links For Software Developers

Saturday, April 28, 2012 0   8394  

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

Sunday, May 01, 2011 0   8816   5

Converting String From Turkish To English

Sunday, December 19, 2010 0   2652  

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

Sunday, October 10, 2010 0   12467  

LINQ

Friday, October 08, 2010 0   3549  

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

Saturday, May 22, 2010 0   2648   1

Debugging in .NET

Saturday, February 13, 2010 0   2328  

VS 2010 / .NET 4 Release Candidate

Tuesday, February 09, 2010 0   2482  

Asp.Net Application Warm Up By Using Windows Service

Thursday, February 04, 2010 6   15530   6

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

Saturday, January 23, 2010 0   4516  

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

Saturday, January 23, 2010 0   8970  

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

Saturday, January 23, 2010 1   6866  

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

Tuesday, December 29, 2009 2   5297