FileShare.Read error ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GB

    FileShare.Read error ?

    Hi !

    I have 2 different processes/application. One is writing to a file and
    another is reading from it. For some reason the code doesnt seems to
    work and gives mscorlib.dll IOException error "This file is being used
    by another process".
    Both the applications are in C#.

    P.S. Even if I try to open the file with NotePad (while my server is
    writing data in the file)it gives the same error.

    Here's the code esnippet :

    Writing the Buffer
    *************** *****
    FileStream fs = new FileStream(File Name, FileMode.OpenOr Create,
    FileAccess.Writ e, System.IO.FileS hare.Read);

    StreamWriter sw = new StreamWriter(fs );
    sw.Write(buff, 0, buff.GetLength( 0)); // no. of bytes to write

    Reading the Buffer
    *************** ******
    string FileName = "D:\\dataFile.t xt";
    StreamReader sr = new StreamReader(ne w
    FileStream(File Name,FileMode.O pen,FileAccess. Read));

    int TotalBytesRead = 0;


    int retval = sr.ReadBlock(ma pBuff, 0, mapBuff.GetLeng th(0)); //bytes
    read if(retval > 0)
    {
    TotalBytesRead = TotalBytesRead + retval;

    }
    Thread.Sleep(50 );


    Can anybody point out whats the error ? or is it a BUG ?

    Thanks a lot for the help,
    Gaurav
  • Armin Zingler

    #2
    Re: FileShare.Read error ?

    "GB" <gaurav_bamania @yahoo.co.uk> schrieb[color=blue]
    >
    > I have 2 different processes/application. One is writing to a file
    > and another is reading from it. For some reason the code doesnt seems
    > to work and gives mscorlib.dll IOException error "This file is being
    > used by another process".
    > Both the applications are in C#.[/color]

    I'd try it in the C# group: ;-) This is the VB.Net group.
    microsoft.publi c.dotnet.langua ges.csharp


    --
    Armin

    How to quote and why:



    Comment

    • Cor Ligthert

      #3
      Re: FileShare.Read error ?

      Hi GB,

      Did you send this with a reason to the language VB group or by accident?

      However a file using the streamreader/writer will be locked until it is
      closed.

      Cor


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: FileShare.Read error ?

        * "Cor Ligthert" <notfirstname@p lanet.nl> scripsit:[color=blue]
        > Did you send this with a reason to the language VB group or by accident?
        >
        > However a file using the streamreader/writer will be locked until it is
        > closed.[/color]

        Not necessarily:

        \\\
        Dim fs1 As New System.IO.FileS tream("C:\Log.t xt", FileMode.Open, FileAccess.Read , FileShare.ReadW rite)
        Dim fs2 As New System.IO.FileS tream("C:\Log.t xt", FileMode.Open, FileAccess.Read , FileShare.ReadW rite)
        Dim sr1 As New System.IO.Strea mReader(fs1)
        Dim sr2 As New System.IO.Strea mReader(fs2)
        MsgBox(sr1.Read Line())
        MsgBox(sr2.Read Line())
        sr1.Close()
        sr2.Close()
        ///

        --
        Herfried K. Wagner [MVP]
        <URL:http://dotnet.mvps.org/>

        Comment

        • Cor Ligthert

          #5
          Re: FileShare.Read error ?

          Hi Herfried,

          Did not look that well, you know I do not like to give a redirect answer
          when I have the idea that I can give an answer.

          Thank you for the message, keeps me attent..

          Cor


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: FileShare.Read error ?

            * "Cor Ligthert" <notfirstname@p lanet.nl> scripsit:[color=blue]
            > Did not look that well, you know I do not like to give a redirect answer
            > when I have the idea that I can give an answer.
            >
            > Thank you for the message, keeps me attent..[/color]

            I didn't know that too, before testing it. Then I made a typo and typed
            'fs1' instead of 'fs2' and thought that reading the same file using 2
            streamreaders didn't work. I think I should take more sleep.

            --
            Herfried K. Wagner [MVP]
            <URL:http://dotnet.mvps.org/>

            Comment

            Working...