1. The process cannot access the file 'c:\temp\Query.txt' because it is being used by another process.

    Try the following code, however be very careful since you could end up reading the contents of the same file.

    private string ReadFileContents(string fileName)
    {
     string sourceText = string.Empty;
     try
     {
      //The next line will give an error if the file is already open by some other process.
      //using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))

      //The next line will work even if the file is open by another process. 
      //However, be careful since you can be reading the contents of the same file again and again.
      using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
      {
       using (StreamReader sr = new StreamReader(fs, Encoding.ASCII))
       {
        sourceText = sr.ReadToEnd();
       }
      }
     }
     catch (Exception ex)
     {
      MessageBox.Show(ex.Message);
     }
     return sourceText;
    }
    0

    Add a comment

Blog Archive
Topics
Topics
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.