Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem opening and reading file [modified]

Problem opening and reading file [modified]

Scheduled Pinned Locked Moved C#
helpcsharp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jjvainav
    wrote on last edited by
    #1

    I am trying to read text from a file, however, I keep getting "The process cannot access the file ... because it is being used by another process." The issue is, I know that another process has the file open, but I need to be able to read the file without closing that process. The weird thing is, I can open and edit the text file with notepad, but all the methods I have tried thus far in C# keep throwing the access error. Here is one of the ways I am trying to open the file: FileStream file; TextReader reader; try { file = new FileStream("C:\\file.log", FileMode.Open, FileAccess.Read, FileShare.Read); reader = new StreamReader(file); string test = reader.ReadLine(); MessageBox.Show(test); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } -- modified at 11:55 Friday 16th June, 2006

    C B 2 Replies Last reply
    0
    • J jjvainav

      I am trying to read text from a file, however, I keep getting "The process cannot access the file ... because it is being used by another process." The issue is, I know that another process has the file open, but I need to be able to read the file without closing that process. The weird thing is, I can open and edit the text file with notepad, but all the methods I have tried thus far in C# keep throwing the access error. Here is one of the ways I am trying to open the file: FileStream file; TextReader reader; try { file = new FileStream("C:\\file.log", FileMode.Open, FileAccess.Read, FileShare.Read); reader = new StreamReader(file); string test = reader.ReadLine(); MessageBox.Show(test); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } -- modified at 11:55 Friday 16th June, 2006

      C Offline
      C Offline
      Chikuu
      wrote on last edited by
      #2

      after reading a line put reader.Close(); and then try

      J 1 Reply Last reply
      0
      • C Chikuu

        after reading a line put reader.Close(); and then try

        J Offline
        J Offline
        jjvainav
        wrote on last edited by
        #3

        It doesn't even make it that far. The error is thrown when I try opening the stream.

        R 1 Reply Last reply
        0
        • J jjvainav

          It doesn't even make it that far. The error is thrown when I try opening the stream.

          R Offline
          R Offline
          Rizwan Majeed
          wrote on last edited by
          #4

          This code seems me ok, I think there could be some issues related to file/system security. Riz

          1 Reply Last reply
          0
          • J jjvainav

            I am trying to read text from a file, however, I keep getting "The process cannot access the file ... because it is being used by another process." The issue is, I know that another process has the file open, but I need to be able to read the file without closing that process. The weird thing is, I can open and edit the text file with notepad, but all the methods I have tried thus far in C# keep throwing the access error. Here is one of the ways I am trying to open the file: FileStream file; TextReader reader; try { file = new FileStream("C:\\file.log", FileMode.Open, FileAccess.Read, FileShare.Read); reader = new StreamReader(file); string test = reader.ReadLine(); MessageBox.Show(test); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } -- modified at 11:55 Friday 16th June, 2006

            B Offline
            B Offline
            BoneSoft
            wrote on last edited by
            #5

            You're only reading one line, is this code in a loop? Can't help much without seeing the context in which that code executes. Of course there is the possibility that another process is using the file. It's a log file, is there something that's logging to it? Try something like...

            try {
            string text = string.Empty;
            using (StreamReader reader = new StreamReader(@"C:\file.log")) {
            text = reader.ReadToEnd(); // or ReadLine()
            reader.Close();
            }
            } catch {}

            Visit BoneSoft.com

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups