Thursday, September 16, 2010

How to merge two or more files using C#




Please visit my new Web Site https://coderstechzone.com



Few days ago i have a requirement to make a processing engine which will download, decode & staging almost 70k+ ASN files. So that i have developed an ASN Decoder using Turbo C. Then make a DLL to use this decoder in my C# project. Everything is good but one problem is that my processing exe will take almost 6 hours to process all files. After reviewing my source code i found that each time my ASN Decoder DLL open a single file then decode it then close the file & also create a csv file for bulk insert into my database. I took a decission to reduce the time for open & closing a file. How i can do it? I made it by merging two or more or mutiple ASN files in a single file. So that my DLL need to open almost 1/3rd of files than earlier & my procesiing now takes only 2 hours. If you are facing such type of problem then you can follow my way.







C# Source code for merging mutiple files:
private void Form1_Load(object sender, EventArgs e)
        {
            string sASN1 = @"11.asn";
            string sASN2 = @"22.asn";

            FileStream FStream1 = null;
            FileStream FStream2 = null;

            FStream1 = File.Open(sASN1, FileMode.Append);
            FStream2 = File.Open(sASN2, FileMode.Open);
            byte[] FStream2Content = new byte[FStream2.Length];
            fs2.Read(FStream2Content, 0, (int)FStream2.Length);
            FStream1.Write(FStream2Content, 0, (int)FStream2.Length);

            MessageBox.Show("Merging Successfully ended!");

            FStream1.Close();
            FStream2.Close();
        }


Happy coding !!

0 comments:

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash