Working with FileStream Object

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

    Working with FileStream Object

    I am working with the this object as oppose to the
    StreamReader object becuase I need to access a file (to
    find the contents) while an external application is
    updating the file. When I was working with the
    StreamReader object I got a deadlock when I tried reading
    the file while my external application was writing to it.

    So far I am able to create a FileStream object and open
    the file in question (CrawlerBackup. txt).
    But I am unable to write code that will read the contents
    because I don't understand the syntax for the overloaded
    methods.

    FileStream objFileStream = new FileStream
    (FILENAME,FileM ode.Open);

    bjFileStream = File.Open(CopyT oFileName,FileM ode.Open,
    FileAccess.Read ,FileShare.Read );




    Can someone help me read the file contents?

    BeginRead has two overloaded methods both require a Byte[]
    buffer as its first argument. What do I put in this
    parameter? I wish to read the contents not pass in a
    Byte array?




  • Christian T.

    #2
    RE: Working with FileStream Object

    You can use a buffer to store the read text. Nothing like a good example.

    EXAMPLE:
    In order to execute this example, Create a new C# Windows Application
    project. Add the following to the Form: a button and a RichTextBox object.
    Double click on the button and complete the code below. Running the app and
    clicking the button will cause the code to read the file and display it in
    the richtextbox.. Hope this helps.

    private void button1_Click(o bject sender, System.EventArg s e)
    {
    string path = @"C:\CrawlerBac kup.txt";
    richTextBox1.Te xt = ""; // reset the richtextbox

    //Open the stream for reading.
    using (FileStream fs = File.OpenRead(p ath))
    {
    byte[] b = new byte[1024]; // byte buffer to store the read bytes
    UTF8Encoding temp = new UTF8Encoding(tr ue);

    // read the file.
    while (fs.Read(b,0,b. Length) > 0)
    {
    // concatenate the richtextBox with the read buffer.
    richTextBox1.Te xt = richTextBox1.Te xt + temp.GetString( b);
    }
    }
    }



    Cheers,
    Christian T. [MSFT]
    Visual Studio Update Team

    - Please do not reply to this email directly. This email is for newsgroup
    purposes only.
    =============== =============== =============== =============== =============
    This posting is provided "AS IS" with no warranties, and confers no
    rights. Use of included script samples are subject to the terms specified
    at http://www.microsoft.com/info/cpyright.htm

    Note: For the benefit of the community-at-large, all responses to this
    message are best directed to the newsgroup/thread from which
    they originated.
    =============== =============== =============== =============== =============

    --------------------[color=blue]
    > Content-Class: urn:content-classes:message
    > From: "Tom" <dariatj@fhlbci n.com>
    > Sender: "Tom" <dariatj@fhlbci n.com>
    > Subject: Working with FileStream Object
    > Date: Wed, 3 Sep 2003 13:08:01 -0700
    > Lines: 32
    > Message-ID: <047b01c37257$1 3e74270$a301280 a@phx.gbl>
    > MIME-Version: 1.0
    > Content-Type: text/plain;
    > charset="iso-8859-1"
    > Content-Transfer-Encoding: 7bit
    > X-Newsreader: Microsoft CDO for Windows 2000
    > X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    > Thread-Index: AcNyVxPnviMyaLh MSWOxnDC1sZwWhA ==
    > Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
    > Path: cpmsftngxa06.ph x.gbl
    > Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1820 44
    > NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
    > X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
    >
    > I am working with the this object as oppose to the
    > StreamReader object becuase I need to access a file (to
    > find the contents) while an external application is
    > updating the file. When I was working with the
    > StreamReader object I got a deadlock when I tried reading
    > the file while my external application was writing to it.
    >
    > So far I am able to create a FileStream object and open
    > the file in question (CrawlerBackup. txt).
    > But I am unable to write code that will read the contents
    > because I don't understand the syntax for the overloaded
    > methods.
    >
    > FileStream objFileStream = new FileStream
    > (FILENAME,FileM ode.Open);
    >
    > bjFileStream = File.Open(CopyT oFileName,FileM ode.Open,
    > FileAccess.Read ,FileShare.Read );
    >
    >
    >
    >
    > Can someone help me read the file contents?
    >
    > BeginRead has two overloaded methods both require a Byte[]
    > buffer as its first argument. What do I put in this
    > parameter? I wish to read the contents not pass in a
    > Byte array?
    >
    >
    >
    >
    >[/color]

    Comment

    • Tom

      #3
      RE: Working with FileStream Object

      Many thanks. I appreciate your help.

      Tom
      [color=blue]
      >-----Original Message-----
      >You can use a buffer to store the read text. Nothing[/color]
      like a good example.[color=blue]
      >
      >EXAMPLE:
      >In order to execute this example, Create a new C# Windows[/color]
      Application[color=blue]
      >project. Add the following to the Form: a button and a[/color]
      RichTextBox object.[color=blue]
      >Double click on the button and complete the code below.[/color]
      Running the app and[color=blue]
      >clicking the button will cause the code to read the file[/color]
      and display it in[color=blue]
      >the richtextbox.. Hope this helps.
      >
      >private void button1_Click(o bject sender,[/color]
      System.EventArg s e)[color=blue]
      >{
      > string path = @"C:\CrawlerBac kup.txt";
      > richTextBox1.Te xt = ""; // reset the richtextbox
      >
      > //Open the stream for reading.
      > using (FileStream fs = File.OpenRead(p ath))
      > {
      > byte[] b = new byte[1024]; // byte buffer[/color]
      to store the read bytes[color=blue]
      > UTF8Encoding temp = new UTF8Encoding[/color]
      (true);[color=blue]
      >
      > // read the file.
      > while (fs.Read(b,0,b. Length) > 0)
      > {
      > // concatenate the richtextBox[/color]
      with the read buffer.[color=blue]
      > richTextBox1.Te xt =[/color]
      richTextBox1.Te xt + temp.GetString( b);[color=blue]
      > }
      > }
      >}
      >
      >
      >
      >Cheers,
      >Christian T. [MSFT]
      >Visual Studio Update Team
      >
      >- Please do not reply to this email directly. This email[/color]
      is for newsgroup[color=blue]
      >purposes only.
      >============== =============== =============== ==============[/color]
      ===============[color=blue]
      >This posting is provided "AS IS" with no warranties, and[/color]
      confers no[color=blue]
      >rights. Use of included script samples are subject to the[/color]
      terms specified[color=blue]
      >at http://www.microsoft.com/info/cpyright.htm
      >
      >Note: For the benefit of the community-at-large, all[/color]
      responses to this[color=blue]
      >message are best directed to the newsgroup/thread from[/color]
      which[color=blue]
      >they originated.
      >============== =============== =============== ==============[/color]
      ===============[color=blue]
      >
      >--------------------[color=green]
      >> Content-Class: urn:content-classes:message
      >> From: "Tom" <dariatj@fhlbci n.com>
      >> Sender: "Tom" <dariatj@fhlbci n.com>
      >> Subject: Working with FileStream Object
      >> Date: Wed, 3 Sep 2003 13:08:01 -0700
      >> Lines: 32
      >> Message-ID: <047b01c37257$1 3e74270$a301280 a@phx.gbl>
      >> MIME-Version: 1.0
      >> Content-Type: text/plain;
      >> charset="iso-8859-1"
      >> Content-Transfer-Encoding: 7bit
      >> X-Newsreader: Microsoft CDO for Windows 2000
      >> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
      >> Thread-Index: AcNyVxPnviMyaLh MSWOxnDC1sZwWhA ==
      >> Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
      >> Path: cpmsftngxa06.ph x.gbl
      >> Xref: cpmsftngxa06.ph x.gbl[/color][/color]
      microsoft.publi c.dotnet.langua ges.csharp:1820 44[color=blue][color=green]
      >> NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
      >> X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
      >>
      >> I am working with the this object as oppose to the
      >> StreamReader object becuase I need to access a file (to
      >> find the contents) while an external application is
      >> updating the file. When I was working with the
      >> StreamReader object I got a deadlock when I tried[/color][/color]
      reading[color=blue][color=green]
      >> the file while my external application was writing to[/color][/color]
      it.[color=blue][color=green]
      >>
      >> So far I am able to create a FileStream object and open
      >> the file in question (CrawlerBackup. txt).
      >> But I am unable to write code that will read the[/color][/color]
      contents[color=blue][color=green]
      >> because I don't understand the syntax for the[/color][/color]
      overloaded[color=blue][color=green]
      >> methods.
      >>
      >> FileStream objFileStream = new FileStream
      >> (FILENAME,FileM ode.Open);
      >>
      >> bjFileStream = File.Open(CopyT oFileName,FileM ode.Open,
      >> FileAccess.Read ,FileShare.Read );
      >>
      >>
      >>
      >>
      >> Can someone help me read the file contents?
      >>
      >> BeginRead has two overloaded methods both require a Byte[/color][/color]
      [][color=blue][color=green]
      >> buffer as its first argument. What do I put in this
      >> parameter? I wish to read the contents not pass in a
      >> Byte array?
      >>
      >>
      >>
      >>
      >>[/color]
      >
      >.
      >[/color]

      Comment

      • Tom

        #4
        RE: Working with FileStream Object

        Christian

        One additional question

        What is the purpose of the @ in

        string path = @"C:\CrawlerBac kup.txt";


        Thanks

        Tom

        [color=blue]
        >-----Original Message-----
        >You can use a buffer to store the read text. Nothing[/color]
        like a good example.[color=blue]
        >
        >EXAMPLE:
        >In order to execute this example, Create a new C# Windows[/color]
        Application[color=blue]
        >project. Add the following to the Form: a button and a[/color]
        RichTextBox object.[color=blue]
        >Double click on the button and complete the code below.[/color]
        Running the app and[color=blue]
        >clicking the button will cause the code to read the file[/color]
        and display it in[color=blue]
        >the richtextbox.. Hope this helps.
        >
        >private void button1_Click(o bject sender,[/color]
        System.EventArg s e)[color=blue]
        >{
        > string path = @"C:\CrawlerBac kup.txt";
        > richTextBox1.Te xt = ""; // reset the richtextbox
        >
        > //Open the stream for reading.
        > using (FileStream fs = File.OpenRead(p ath))
        > {
        > byte[] b = new byte[1024]; // byte buffer[/color]
        to store the read bytes[color=blue]
        > UTF8Encoding temp = new UTF8Encoding[/color]
        (true);[color=blue]
        >
        > // read the file.
        > while (fs.Read(b,0,b. Length) > 0)
        > {
        > // concatenate the richtextBox[/color]
        with the read buffer.[color=blue]
        > richTextBox1.Te xt =[/color]
        richTextBox1.Te xt + temp.GetString( b);[color=blue]
        > }
        > }
        >}
        >
        >
        >
        >Cheers,
        >Christian T. [MSFT]
        >Visual Studio Update Team
        >
        >- Please do not reply to this email directly. This email[/color]
        is for newsgroup[color=blue]
        >purposes only.
        >============== =============== =============== ==============[/color]
        ===============[color=blue]
        >This posting is provided "AS IS" with no warranties, and[/color]
        confers no[color=blue]
        >rights. Use of included script samples are subject to the[/color]
        terms specified[color=blue]
        >at http://www.microsoft.com/info/cpyright.htm
        >
        >Note: For the benefit of the community-at-large, all[/color]
        responses to this[color=blue]
        >message are best directed to the newsgroup/thread from[/color]
        which[color=blue]
        >they originated.
        >============== =============== =============== ==============[/color]
        ===============[color=blue]
        >
        >--------------------[color=green]
        >> Content-Class: urn:content-classes:message
        >> From: "Tom" <dariatj@fhlbci n.com>
        >> Sender: "Tom" <dariatj@fhlbci n.com>
        >> Subject: Working with FileStream Object
        >> Date: Wed, 3 Sep 2003 13:08:01 -0700
        >> Lines: 32
        >> Message-ID: <047b01c37257$1 3e74270$a301280 a@phx.gbl>
        >> MIME-Version: 1.0
        >> Content-Type: text/plain;
        >> charset="iso-8859-1"
        >> Content-Transfer-Encoding: 7bit
        >> X-Newsreader: Microsoft CDO for Windows 2000
        >> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
        >> Thread-Index: AcNyVxPnviMyaLh MSWOxnDC1sZwWhA ==
        >> Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
        >> Path: cpmsftngxa06.ph x.gbl
        >> Xref: cpmsftngxa06.ph x.gbl[/color][/color]
        microsoft.publi c.dotnet.langua ges.csharp:1820 44[color=blue][color=green]
        >> NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
        >> X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
        >>
        >> I am working with the this object as oppose to the
        >> StreamReader object becuase I need to access a file (to
        >> find the contents) while an external application is
        >> updating the file. When I was working with the
        >> StreamReader object I got a deadlock when I tried[/color][/color]
        reading[color=blue][color=green]
        >> the file while my external application was writing to[/color][/color]
        it.[color=blue][color=green]
        >>
        >> So far I am able to create a FileStream object and open
        >> the file in question (CrawlerBackup. txt).
        >> But I am unable to write code that will read the[/color][/color]
        contents[color=blue][color=green]
        >> because I don't understand the syntax for the[/color][/color]
        overloaded[color=blue][color=green]
        >> methods.
        >>
        >> FileStream objFileStream = new FileStream
        >> (FILENAME,FileM ode.Open);
        >>
        >> bjFileStream = File.Open(CopyT oFileName,FileM ode.Open,
        >> FileAccess.Read ,FileShare.Read );
        >>
        >>
        >>
        >>
        >> Can someone help me read the file contents?
        >>
        >> BeginRead has two overloaded methods both require a Byte[/color][/color]
        [][color=blue][color=green]
        >> buffer as its first argument. What do I put in this
        >> parameter? I wish to read the contents not pass in a
        >> Byte array?
        >>
        >>
        >>
        >>
        >>[/color]
        >
        >.
        >[/color]

        Comment

        • Tom

          #5
          RE: Working with FileStream Object

          Dear Christian:

          I'm very sorry but I have one more question...

          I hope you don't mind.

          Is it your understanding that using FileStream object over
          the StreamReader will allow me to read a file while
          another application cn be updating it at the same time?

          See I went down the path of useing the StreamReader object
          but found it would lock the file when I either read it or
          copied it. This caused the other application (which
          writes to it) to hang up.

          Since I can't have this I found this object has
          Asynchonronizat ion.

          Am I on the right track???[color=blue]
          >-----Original Message-----
          >You can use a buffer to store the read text. Nothing[/color]
          like a good example.[color=blue]
          >
          >EXAMPLE:
          >In order to execute this example, Create a new C# Windows[/color]
          Application[color=blue]
          >project. Add the following to the Form: a button and a[/color]
          RichTextBox object.[color=blue]
          >Double click on the button and complete the code below.[/color]
          Running the app and[color=blue]
          >clicking the button will cause the code to read the file[/color]
          and display it in[color=blue]
          >the richtextbox.. Hope this helps.
          >
          >private void button1_Click(o bject sender,[/color]
          System.EventArg s e)[color=blue]
          >{
          > string path = @"C:\CrawlerBac kup.txt";
          > richTextBox1.Te xt = ""; // reset the richtextbox
          >
          > //Open the stream for reading.
          > using (FileStream fs = File.OpenRead(p ath))
          > {
          > byte[] b = new byte[1024]; // byte buffer[/color]
          to store the read bytes[color=blue]
          > UTF8Encoding temp = new UTF8Encoding[/color]
          (true);[color=blue]
          >
          > // read the file.
          > while (fs.Read(b,0,b. Length) > 0)
          > {
          > // concatenate the richtextBox[/color]
          with the read buffer.[color=blue]
          > richTextBox1.Te xt =[/color]
          richTextBox1.Te xt + temp.GetString( b);[color=blue]
          > }
          > }
          >}
          >
          >
          >
          >Cheers,
          >Christian T. [MSFT]
          >Visual Studio Update Team
          >
          >- Please do not reply to this email directly. This email[/color]
          is for newsgroup[color=blue]
          >purposes only.
          >============== =============== =============== ==============[/color]
          ===============[color=blue]
          >This posting is provided "AS IS" with no warranties, and[/color]
          confers no[color=blue]
          >rights. Use of included script samples are subject to the[/color]
          terms specified[color=blue]
          >at http://www.microsoft.com/info/cpyright.htm
          >
          >Note: For the benefit of the community-at-large, all[/color]
          responses to this[color=blue]
          >message are best directed to the newsgroup/thread from[/color]
          which[color=blue]
          >they originated.
          >============== =============== =============== ==============[/color]
          ===============[color=blue]
          >
          >--------------------[color=green]
          >> Content-Class: urn:content-classes:message
          >> From: "Tom" <dariatj@fhlbci n.com>
          >> Sender: "Tom" <dariatj@fhlbci n.com>
          >> Subject: Working with FileStream Object
          >> Date: Wed, 3 Sep 2003 13:08:01 -0700
          >> Lines: 32
          >> Message-ID: <047b01c37257$1 3e74270$a301280 a@phx.gbl>
          >> MIME-Version: 1.0
          >> Content-Type: text/plain;
          >> charset="iso-8859-1"
          >> Content-Transfer-Encoding: 7bit
          >> X-Newsreader: Microsoft CDO for Windows 2000
          >> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
          >> Thread-Index: AcNyVxPnviMyaLh MSWOxnDC1sZwWhA ==
          >> Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
          >> Path: cpmsftngxa06.ph x.gbl
          >> Xref: cpmsftngxa06.ph x.gbl[/color][/color]
          microsoft.publi c.dotnet.langua ges.csharp:1820 44[color=blue][color=green]
          >> NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
          >> X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
          >>
          >> I am working with the this object as oppose to the
          >> StreamReader object becuase I need to access a file (to
          >> find the contents) while an external application is
          >> updating the file. When I was working with the
          >> StreamReader object I got a deadlock when I tried[/color][/color]
          reading[color=blue][color=green]
          >> the file while my external application was writing to[/color][/color]
          it.[color=blue][color=green]
          >>
          >> So far I am able to create a FileStream object and open
          >> the file in question (CrawlerBackup. txt).
          >> But I am unable to write code that will read the[/color][/color]
          contents[color=blue][color=green]
          >> because I don't understand the syntax for the[/color][/color]
          overloaded[color=blue][color=green]
          >> methods.
          >>
          >> FileStream objFileStream = new FileStream
          >> (FILENAME,FileM ode.Open);
          >>
          >> bjFileStream = File.Open(CopyT oFileName,FileM ode.Open,
          >> FileAccess.Read ,FileShare.Read );
          >>
          >>
          >>
          >>
          >> Can someone help me read the file contents?
          >>
          >> BeginRead has two overloaded methods both require a Byte[/color][/color]
          [][color=blue][color=green]
          >> buffer as its first argument. What do I put in this
          >> parameter? I wish to read the contents not pass in a
          >> Byte array?
          >>
          >>
          >>
          >>
          >>[/color]
          >
          >.
          >[/color]

          Comment

          • David Browne

            #6
            Re: Working with FileStream Object


            "Tom" <dariatj@fhlbci n.com> wrote in message
            news:057a01c372 62$060e0750$a30 1280a@phx.gbl.. .[color=blue]
            > Dear Christian:
            >
            > I'm very sorry but I have one more question...
            >
            > I hope you don't mind.
            >
            > Is it your understanding that using FileStream object over
            > the StreamReader will allow me to read a file while
            > another application cn be updating it at the same time?
            >
            > See I went down the path of useing the StreamReader object
            > but found it would lock the file when I either read it or
            > copied it. This caused the other application (which
            > writes to it) to hang up.
            >[/color]

            FileStream just gives you more low-level control over how you open and read
            the file.

            Once you have opened the filestream with whatever kind of access and sharing
            you want, you can create a new StreamReader to read from the FileStream.

            IO.FileStream fs = new IO.FileStream(" c:\foo.txt", IO.FileMode.Ope n,
            IO.FileAccess.R ead);
            IO.StreamReader sr = New IO.StreamReader (fs);

            Then just use the StreamReader. Best of both worlds.

            David


            Comment

            • Tom

              #7
              Re: Working with FileStream Object

              Thanks David for your tip. I never thought about mixing
              the objects together.

              But I'm puzzled about something. Is their an advantage to
              your technique and do I have to close both objects? Will
              this approach work when an external application is
              updating the file while I'm trying to read it?

              Thanks

              Tom
              Also, do you know what the @ does in does??
              string path = @"C:\CrawlerBac kup.txt";
              [color=blue]
              >-----Original Message-----
              >
              >"Tom" <dariatj@fhlbci n.com> wrote in message
              >news:057a01c37 262$060e0750$a3 01280a@phx.gbl. ..[color=green]
              >> Dear Christian:
              >>
              >> I'm very sorry but I have one more question...
              >>
              >> I hope you don't mind.
              >>
              >> Is it your understanding that using FileStream object[/color][/color]
              over[color=blue][color=green]
              >> the StreamReader will allow me to read a file while
              >> another application cn be updating it at the same time?
              >>
              >> See I went down the path of useing the StreamReader[/color][/color]
              object[color=blue][color=green]
              >> but found it would lock the file when I either read it[/color][/color]
              or[color=blue][color=green]
              >> copied it. This caused the other application (which
              >> writes to it) to hang up.
              >>[/color]
              >
              >FileStream just gives you more low-level control over how[/color]
              you open and read[color=blue]
              >the file.
              >
              >Once you have opened the filestream with whatever kind of[/color]
              access and sharing[color=blue]
              >you want, you can create a new StreamReader to read from[/color]
              the FileStream.[color=blue]
              >
              >IO.FileStrea m fs = new IO.FileStream(" c:\foo.txt",[/color]
              IO.FileMode.Ope n,[color=blue]
              >IO.FileAccess. Read);
              >IO.StreamReade r sr = New IO.StreamReader (fs);
              >
              >Then just use the StreamReader. Best of both worlds.
              >
              >David
              >
              >
              >.
              >[/color]

              Comment

              • Justin Rogers

                #8
                Re: Working with FileStream Object

                Mixing a Stream and a Reader, you only need to close the Reader and it
                closes the underlying stream. This can actually be a pain since there are
                cases where you might want to use different types of readers on the same
                underlying stream (read some text, then read some binary for instance), but
                there was a smal oversight for this when creating the IO classes.

                There is an advantage to using a Stream and a Reader. You can explicitly
                call any special overloads on the stream. This option isn't available when
                you just use a reader. Internally the reader simply allocates a FileStream
                with the most often used set of parameters. In your case, the FileStream
                was being automatically created using on the filename and whatever the
                default values are for the rest of the FileStream.


                --
                Justin Rogers
                DigiTec Web Consultants, LLC.

                "Tom" <dariatj@fhlbci n.com> wrote in message
                news:05f301c372 68$aedd4d90$a30 1280a@phx.gbl.. .[color=blue]
                > Thanks David for your tip. I never thought about mixing
                > the objects together.
                >
                > But I'm puzzled about something. Is their an advantage to
                > your technique and do I have to close both objects? Will
                > this approach work when an external application is
                > updating the file while I'm trying to read it?
                >
                > Thanks
                >
                > Tom
                > Also, do you know what the @ does in does??
                > string path = @"C:\CrawlerBac kup.txt";
                >[color=green]
                > >-----Original Message-----
                > >
                > >"Tom" <dariatj@fhlbci n.com> wrote in message
                > >news:057a01c37 262$060e0750$a3 01280a@phx.gbl. ..[color=darkred]
                > >> Dear Christian:
                > >>
                > >> I'm very sorry but I have one more question...
                > >>
                > >> I hope you don't mind.
                > >>
                > >> Is it your understanding that using FileStream object[/color][/color]
                > over[color=green][color=darkred]
                > >> the StreamReader will allow me to read a file while
                > >> another application cn be updating it at the same time?
                > >>
                > >> See I went down the path of useing the StreamReader[/color][/color]
                > object[color=green][color=darkred]
                > >> but found it would lock the file when I either read it[/color][/color]
                > or[color=green][color=darkred]
                > >> copied it. This caused the other application (which
                > >> writes to it) to hang up.
                > >>[/color]
                > >
                > >FileStream just gives you more low-level control over how[/color]
                > you open and read[color=green]
                > >the file.
                > >
                > >Once you have opened the filestream with whatever kind of[/color]
                > access and sharing[color=green]
                > >you want, you can create a new StreamReader to read from[/color]
                > the FileStream.[color=green]
                > >
                > >IO.FileStrea m fs = new IO.FileStream(" c:\foo.txt",[/color]
                > IO.FileMode.Ope n,[color=green]
                > >IO.FileAccess. Read);
                > >IO.StreamReade r sr = New IO.StreamReader (fs);
                > >
                > >Then just use the StreamReader. Best of both worlds.
                > >
                > >David
                > >
                > >
                > >.
                > >[/color][/color]


                Comment

                • Tom

                  #9
                  Re: Working with FileStream Object

                  David:

                  While I understand about the no guarantees... I wanted to
                  provide more information.
                  The other application is simply updating itself with
                  progress. The other application is MondSearch and it
                  updates this file when an incremental or full reindex is
                  initiated by the end user.

                  They change the status from Init to Grab to Pubish to
                  Complete. If their is an error the message will be Error
                  along with an explanation.

                  All I'm trying to do is "read" the file and in some case
                  the file may be being updated while I attempt to read it.

                  So, I found the StreamReader object to lock the file while
                  I was trying to read it thus disallowing the other
                  application to update it, thereby locking the search
                  update.

                  I've been told by the vendor that they update the file
                  with read write access. So I'm hoping using the
                  FileStream will help solve this.

                  Tom
                  [color=blue]
                  >-----Original Message-----
                  >
                  >"Tom" <dariatj@fhlbci n.com> wrote in message
                  >news:05f301c37 268$aedd4d90$a3 01280a@phx.gbl. ..[color=green]
                  >> Thanks David for your tip. I never thought about mixing
                  >> the objects together.
                  >>
                  >> But I'm puzzled about something. Is their an advantage[/color][/color]
                  to[color=blue][color=green]
                  >> your technique and do I have to close both objects?[/color]
                  >
                  >Closing the StreamReader will close the underlying stream.
                  >[color=green]
                  >> Will
                  >> this approach work when an external application is
                  >> updating the file while I'm trying to read it?[/color]
                  >
                  >No guarantee here. It depends on whether the other[/color]
                  application is willing[color=blue]
                  >to share access to the file
                  >and what sort of changes the other app is making to the[/color]
                  file.[color=blue]
                  >
                  >David
                  >
                  >
                  >.
                  >[/color]

                  Comment

                  • Joshua Coady

                    #10
                    Re: Working with FileStream Object

                    The @ sign turns off escape sequences like \n, \r, \t etc and allows you to
                    have line breaks in the string.

                    This

                    string str = "line one\r\nline two, \"text in quotes\"";

                    is equivalent to this:

                    string str = @"line one
                    line two, ""text in quotes""";

                    This is most useful for string like paths where you have a lot of '\'
                    characters
                    (i.e. this @"c:\dir\sub\fi le.txt" is nicer than this
                    "c:\\dir\\sub\\ file.txt")

                    Josh


                    "Tom" <dariatj@fhlbci n.com> wrote in message
                    news:068601c372 60$b18d9a20$a10 1280a@phx.gbl.. .[color=blue]
                    > Christian
                    >
                    > One additional question
                    >
                    > What is the purpose of the @ in
                    >
                    > string path = @"C:\CrawlerBac kup.txt";
                    >
                    >
                    > Thanks
                    >
                    > Tom
                    >
                    >[color=green]
                    > >-----Original Message-----
                    > >You can use a buffer to store the read text. Nothing[/color]
                    > like a good example.[color=green]
                    > >
                    > >EXAMPLE:
                    > >In order to execute this example, Create a new C# Windows[/color]
                    > Application[color=green]
                    > >project. Add the following to the Form: a button and a[/color]
                    > RichTextBox object.[color=green]
                    > >Double click on the button and complete the code below.[/color]
                    > Running the app and[color=green]
                    > >clicking the button will cause the code to read the file[/color]
                    > and display it in[color=green]
                    > >the richtextbox.. Hope this helps.
                    > >
                    > >private void button1_Click(o bject sender,[/color]
                    > System.EventArg s e)[color=green]
                    > >{
                    > > string path = @"C:\CrawlerBac kup.txt";
                    > > richTextBox1.Te xt = ""; // reset the richtextbox
                    > >
                    > > //Open the stream for reading.
                    > > using (FileStream fs = File.OpenRead(p ath))
                    > > {
                    > > byte[] b = new byte[1024]; // byte buffer[/color]
                    > to store the read bytes[color=green]
                    > > UTF8Encoding temp = new UTF8Encoding[/color]
                    > (true);[color=green]
                    > >
                    > > // read the file.
                    > > while (fs.Read(b,0,b. Length) > 0)
                    > > {
                    > > // concatenate the richtextBox[/color]
                    > with the read buffer.[color=green]
                    > > richTextBox1.Te xt =[/color]
                    > richTextBox1.Te xt + temp.GetString( b);[color=green]
                    > > }
                    > > }
                    > >}
                    > >
                    > >
                    > >
                    > >Cheers,
                    > >Christian T. [MSFT]
                    > >Visual Studio Update Team
                    > >
                    > >- Please do not reply to this email directly. This email[/color]
                    > is for newsgroup[color=green]
                    > >purposes only.
                    > >============== =============== =============== ==============[/color]
                    > ===============[color=green]
                    > >This posting is provided "AS IS" with no warranties, and[/color]
                    > confers no[color=green]
                    > >rights. Use of included script samples are subject to the[/color]
                    > terms specified[color=green]
                    > >at http://www.microsoft.com/info/cpyright.htm
                    > >
                    > >Note: For the benefit of the community-at-large, all[/color]
                    > responses to this[color=green]
                    > >message are best directed to the newsgroup/thread from[/color]
                    > which[color=green]
                    > >they originated.
                    > >============== =============== =============== ==============[/color]
                    > ===============[color=green]
                    > >
                    > >--------------------[color=darkred]
                    > >> Content-Class: urn:content-classes:message
                    > >> From: "Tom" <dariatj@fhlbci n.com>
                    > >> Sender: "Tom" <dariatj@fhlbci n.com>
                    > >> Subject: Working with FileStream Object
                    > >> Date: Wed, 3 Sep 2003 13:08:01 -0700
                    > >> Lines: 32
                    > >> Message-ID: <047b01c37257$1 3e74270$a301280 a@phx.gbl>
                    > >> MIME-Version: 1.0
                    > >> Content-Type: text/plain;
                    > >> charset="iso-8859-1"
                    > >> Content-Transfer-Encoding: 7bit
                    > >> X-Newsreader: Microsoft CDO for Windows 2000
                    > >> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
                    > >> Thread-Index: AcNyVxPnviMyaLh MSWOxnDC1sZwWhA ==
                    > >> Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
                    > >> Path: cpmsftngxa06.ph x.gbl
                    > >> Xref: cpmsftngxa06.ph x.gbl[/color][/color]
                    > microsoft.publi c.dotnet.langua ges.csharp:1820 44[color=green][color=darkred]
                    > >> NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
                    > >> X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
                    > >>
                    > >> I am working with the this object as oppose to the
                    > >> StreamReader object becuase I need to access a file (to
                    > >> find the contents) while an external application is
                    > >> updating the file. When I was working with the
                    > >> StreamReader object I got a deadlock when I tried[/color][/color]
                    > reading[color=green][color=darkred]
                    > >> the file while my external application was writing to[/color][/color]
                    > it.[color=green][color=darkred]
                    > >>
                    > >> So far I am able to create a FileStream object and open
                    > >> the file in question (CrawlerBackup. txt).
                    > >> But I am unable to write code that will read the[/color][/color]
                    > contents[color=green][color=darkred]
                    > >> because I don't understand the syntax for the[/color][/color]
                    > overloaded[color=green][color=darkred]
                    > >> methods.
                    > >>
                    > >> FileStream objFileStream = new FileStream
                    > >> (FILENAME,FileM ode.Open);
                    > >>
                    > >> bjFileStream = File.Open(CopyT oFileName,FileM ode.Open,
                    > >> FileAccess.Read ,FileShare.Read );
                    > >>
                    > >>
                    > >>
                    > >>
                    > >> Can someone help me read the file contents?
                    > >>
                    > >> BeginRead has two overloaded methods both require a Byte[/color][/color]
                    > [][color=green][color=darkred]
                    > >> buffer as its first argument. What do I put in this
                    > >> parameter? I wish to read the contents not pass in a
                    > >> Byte array?
                    > >>
                    > >>
                    > >>
                    > >>
                    > >>[/color]
                    > >
                    > >.
                    > >[/color][/color]


                    Comment

                    Working...