none
How to read and then write a very big file using FileStream in C#.Net?

    Question

  • I am developing a windows application using C#.Net 2005.
    In this I want to read a file from one location and then write it to another location using FileStream.
    The code that I have writeen is -

    System.IO.FileStream oFileStream;
    System.IO.FileInfo oFile;
    oFile = new System.IO.FileInfo(sFilePath);
    oFileStream = oFile.OpenRead();

    int lBytes = Convert.ToInt32(oFileStream.Length);
    byte[] fileData = new byte[lBytes];
    oFileStream.Read(fileData, 0, lBytes);
    oFileStream.Close();

    System.IO.FileStream oNewFileStream;
    oNewFileStream = new System.IO.FileStream(sFilePath, FileMode.CreateNew);
    oNewFileStream.Write(fileData, 0, fileData.Length);
    oNewFileStream.Close();

    the above code works fine for small files.
    but if the file size is very large then the code - int lBytes = Convert.ToInt32(oFileStream.Length);
    gives error as it cannot convert a big length to int.
    Also if I use a long datatype as - long lBytes ;
    But in Read method it requires int datatype only.
    How to solve this problem? And I have to do this using Filestream only.

    Saturday, February 16, 2008 1:09 PM

Answers

  • You have to use buffering Wink

    Try this:

    Code Snippet

                System.IO.FileStream oFileStream;
                System.IO.FileStream oNewFileStream;
                oNewFileStream = new System.IO.FileStream(sFilePath, FileMode.CreateNew);
               
                System.IO.FileInfo oFile;
                oFile = new System.IO.FileInfo(sFilePath);
               
                oFileStream = oFile.OpenRead();

                byte[] fileData = new byte[256]; // buffer
                int lBytes = oFileStream.Read(fileData, 0, fileData.Length);

                while (lBytes != 0)
                {
                    oNewFileStream.Write(fileData, 0, lBytes);
                    lBytes = oFileStream.Read(fileData, 0, fileData.Length);
                }   
               
                oFileStream.Close();
                oNewFileStream.Close();



    Regards.
    Sunday, February 17, 2008 11:39 AM

All replies

  •  

    Ok, if noone is getting answer for above question then atleast tell me can I read That filestream byte by byte and then similarly write it.

    Byte by byte means I will define a byte as -

    byte[1024] filedata;

    and then read this much length and write it.

    Sunday, February 17, 2008 6:42 AM
  • You have to use buffering Wink

    Try this:

    Code Snippet

                System.IO.FileStream oFileStream;
                System.IO.FileStream oNewFileStream;
                oNewFileStream = new System.IO.FileStream(sFilePath, FileMode.CreateNew);
               
                System.IO.FileInfo oFile;
                oFile = new System.IO.FileInfo(sFilePath);
               
                oFileStream = oFile.OpenRead();

                byte[] fileData = new byte[256]; // buffer
                int lBytes = oFileStream.Read(fileData, 0, fileData.Length);

                while (lBytes != 0)
                {
                    oNewFileStream.Write(fileData, 0, lBytes);
                    lBytes = oFileStream.Read(fileData, 0, fileData.Length);
                }   
               
                oFileStream.Close();
                oNewFileStream.Close();



    Regards.
    Sunday, February 17, 2008 11:39 AM
  • My requirement is to read a word doc from asp.net 2.0 and display it in rdlc report text box .

    I was using the below code .Which works fine .But it is not losses the formating like Bold ,under line ,

    Paragraph

    ..etc

    Word.Application obj_app = new Word.ApplicationClass();
                Word.Document obj_doc = new Word.DocumentClass();
                object readOnly = false;
                object isVisible = true;
                object missing = System.Reflection.Missing.Value;
                object filepath = (@"c:\PrintedPlan.doc"); //file to be read
                obj_doc = obj_app.Documents.Open(ref filepath, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
                obj_doc.Select();
                string input = obj_app.Selection.FormattedText.Text;
                TextBox1.Text = input;

                ReportParameter[] parms = new ReportParameter[1];
                parms[0] = new ReportParameter("fx", input);
                ReportViewer1.LocalReport.SetParameters(parms);
                obj_doc.Close(ref missing, ref missing, ref missing);
                obj_app.Quit(ref missing, ref missing, ref missing);

    Is there any nice way of reading word with out lossing the formats?

     Thanks in advance.

    Wednesday, May 14, 2008 2:25 PM

  • Hi All,

    I am interested in the topic. I am trying to read 15GB of binary data in C#. This is not cacheable because it's out of memory.
    Could anyone advice how we can read such a big file in the fast way?

    Any advice would be highly appreciated!


    Regards,

    LG
    Monday, June 16, 2008 1:08 PM
  •  

    Ok, you need to us Async IO and read or write chunks at a time.  This gives you two advatiges. One being you can updata a status bar for the use and secound you can shovel the data onto sumthing and see if there is enuf space left.  I don’t have any example I can send you unfortinuntly.  Look for Async IO example with recursave call’s into BeginRead.

    Hope this helps

    Friday, July 25, 2008 4:19 PM