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. Read Large File

Read Large File

Scheduled Pinned Locked Moved C#
csharphelptutorial
12 Posts 6 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.
  • R Reza Shojaee

    Hi everybody I have a problem for open large file in C#. While I want to open file with 100 MByte size(for example) and read its contents and show in the TextBox, program hanged. If you know any solution about mentioned matter, I will become very happy for your assistance. Thank you

    Best Regards, Reza Shojaee

    G Offline
    G Offline
    Guffa
    wrote on last edited by
    #2

    That is way too much data to display in a TextBox. You need to rethink how you want to display the data. It's not possible to see more than a few kilobytes worth of text on the screen at once anyway.

    Despite everything, the person most likely to be fooling you next is yourself.

    R 1 Reply Last reply
    0
    • G Guffa

      That is way too much data to display in a TextBox. You need to rethink how you want to display the data. It's not possible to see more than a few kilobytes worth of text on the screen at once anyway.

      Despite everything, the person most likely to be fooling you next is yourself.

      R Offline
      R Offline
      Reza Shojaee
      wrote on last edited by
      #3

      Hi Guffa My Problem not TextBox. You suppose I want show data from file line by line into TextBox. My problem is volume of file

      Best Regards, Reza Shojaee

      L G 2 Replies Last reply
      0
      • R Reza Shojaee

        Hi Guffa My Problem not TextBox. You suppose I want show data from file line by line into TextBox. My problem is volume of file

        Best Regards, Reza Shojaee

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        You can read the file in another thread, this way your GUI should stay respondent. But I still wouldn't read it completely into memory, you will run into OutOfMemoryExceptions rather quickly. I'd suggest to read smaller chunks of the file as needed. regards

        realJSOPR 1 Reply Last reply
        0
        • L Lost User

          You can read the file in another thread, this way your GUI should stay respondent. But I still wouldn't read it completely into memory, you will run into OutOfMemoryExceptions rather quickly. I'd suggest to read smaller chunks of the file as needed. regards

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #5

          A) It's only 100mb. He shouldn't be running out of memory. B) When memory fills up, Windows pages to disk. He shouldn't be running out of memory.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          1 Reply Last reply
          0
          • R Reza Shojaee

            Hi everybody I have a problem for open large file in C#. While I want to open file with 100 MByte size(for example) and read its contents and show in the TextBox, program hanged. If you know any solution about mentioned matter, I will become very happy for your assistance. Thank you

            Best Regards, Reza Shojaee

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #6

            A string can theoretically only hold 2,147,483,647 characters. However, on a 32-bit OS, you never have that much contiguous memory available, so you'll probably never be able to create a string that large. Beyond that, no single array or structure can contain more than 1 billion items. If you're running a 64-bit OS, you will realize larger strings, but you still might not be able to allocate their full size unless you have 4-8gb of RAM. So, your problem is fragmented memory. With .Net, I'm not sure there's much you can do about it beyond allocating the string at program startup and hope you have enough unfragmented memory at that point. I would also use a StringBuilder object instead of a string because the constructor for a StringBuilder allows you to specify the size of the object, and you can put a try/catch block around the constructor call to make sure the framework didn't have any allocation problems.

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            modified on Sunday, August 10, 2008 7:23 AM

            1 Reply Last reply
            0
            • R Reza Shojaee

              Hi everybody I have a problem for open large file in C#. While I want to open file with 100 MByte size(for example) and read its contents and show in the TextBox, program hanged. If you know any solution about mentioned matter, I will become very happy for your assistance. Thank you

              Best Regards, Reza Shojaee

              R Offline
              R Offline
              Reza Shojaee
              wrote on last edited by
              #7

              Hi my friend I think my problem with StreamReader solved StreamReader sr = new StreamReader("c:\\1.psd"); string line; while ((line = sr.ReadLine()) != null) thank you for your helping

              Best Regards, Reza Shojaee

              M 1 Reply Last reply
              0
              • R Reza Shojaee

                Hi my friend I think my problem with StreamReader solved StreamReader sr = new StreamReader("c:\\1.psd"); string line; while ((line = sr.ReadLine()) != null) thank you for your helping

                Best Regards, Reza Shojaee

                M Offline
                M Offline
                Mohammad Dayyan
                wrote on last edited by
                #8

                Also you can use this :

                List<string> list = new List<string>();
                using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
                {
                //Read lines
                while (sr.Peek() >= 0)
                list.Add(sr.ReadLine());
                }

                G 1 Reply Last reply
                0
                • M Mohammad Dayyan

                  Also you can use this :

                  List<string> list = new List<string>();
                  using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
                  {
                  //Read lines
                  while (sr.Peek() >= 0)
                  list.Add(sr.ReadLine());
                  }

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #9

                  Or simply: string[] list = File.ReadAllLines(openFileDialog1.FileName);

                  Despite everything, the person most likely to be fooling you next is yourself.

                  M 1 Reply Last reply
                  0
                  • R Reza Shojaee

                    Hi Guffa My Problem not TextBox. You suppose I want show data from file line by line into TextBox. My problem is volume of file

                    Best Regards, Reza Shojaee

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #10

                    Reza Shojaee wrote:

                    My Problem not TextBox. You suppose I want show data from file line by line into TextBox. My problem is volume of file

                    Then I don't understand your question. What is the problem exactly? What error message do you get?

                    Despite everything, the person most likely to be fooling you next is yourself.

                    1 Reply Last reply
                    0
                    • R Reza Shojaee

                      Hi everybody I have a problem for open large file in C#. While I want to open file with 100 MByte size(for example) and read its contents and show in the TextBox, program hanged. If you know any solution about mentioned matter, I will become very happy for your assistance. Thank you

                      Best Regards, Reza Shojaee

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #11

                      You rarely need to have an entire file in memory all at once; what are you trying to do?

                      1 Reply Last reply
                      0
                      • G Guffa

                        Or simply: string[] list = File.ReadAllLines(openFileDialog1.FileName);

                        Despite everything, the person most likely to be fooling you next is yourself.

                        M Offline
                        M Offline
                        Mohammad Dayyan
                        wrote on last edited by
                        #12

                        Yeah you're right

                        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