Advertisement
rlemon

Binary Converter Tool

Oct 26th, 2011
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1.         private void importDataToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             openFileDialog.Filter = datFilter;
  4.             if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  5.             {
  6.                 data = new DataTable();
  7.                 ThreadStart s = delegate { getData(openFileDialog.FileName); };
  8.                 Thread t = new Thread(s);
  9.                 t.Start();
  10.             }
  11.         }
  12.  
  13.     private void updateProgressBar(object[] args)
  14.         {
  15.             try
  16.             {
  17.                 if (this.InvokeRequired)
  18.                 {
  19.                     DelegateMethod delegateMethod = new DelegateMethod(updateProgressBar);
  20.                     this.Invoke(delegateMethod, new object[] { args });
  21.                 }
  22.                 else
  23.                 {
  24.                     if ((int)args[0] > 0)
  25.                     {
  26.                         toolStripProgressBar.Value = (int)args[0];
  27.                     }
  28.                     else
  29.                     {
  30.                         toolStripProgressBar.Value = toolStripProgressBar.Maximum;
  31.                     }
  32.                 }
  33.             }
  34.             catch (Exception ex)
  35.             {
  36.                 MessageBox.Show(ex.Message, CONST_STRINGS.MSGBOX_HEADER_ERROR);
  37.             }
  38.         }
  39.  
  40.         private void getData(string filepath)
  41.         {
  42.             try
  43.             {
  44.                 if (File.Exists(filepath))
  45.                 {
  46.                     dataReadStopWatch.Start();
  47.                    
  48.                     BinaryReader bReader = new BinaryReader(File.Open(filepath, FileMode.Open));
  49.  
  50.                     setupProgressBar(new object[] { 0, (int)bReader.BaseStream.Length });
  51.  
  52.                     int columnCount = bReader.ReadInt32();
  53.  
  54.                     data.Columns.Add(CONST_STRINGS.DATA_COLUMN_HEADER_DATETIME);
  55.                    
  56.                     for (int i = 0; i < columnCount; i++)
  57.                     {
  58.                         data.Columns.Add(new string(bReader.ReadChars(bReader.ReadInt32())));
  59.                         updateProgressBar(new object[] { (int)bReader.BaseStream.Position });
  60.                     }
  61.  
  62.                     bool eof = false;
  63.                     while (!eof)
  64.                     {
  65.                         try
  66.                         {
  67.                             List<object> RowData = new List<object>();
  68.                             RowData.Add(bReader.ReadInt32());
  69.                             for (int i = 0; i < columnCount; i++)
  70.                             {
  71.                                 RowData.Add(bReader.ReadSingle().ToString());
  72.                             }
  73.                             data.Rows.Add(RowData.ToArray());
  74.                             updateProgressBar(new object[] { (int)bReader.BaseStream.Position });
  75.  
  76.                         }
  77.                         catch (Exception ex)
  78.                         {
  79.                             eof = true;
  80.                         }
  81.                     }
  82.                 }
  83.                 else
  84.                 {
  85.                     throw new Exception(CONST_STRINGS.ERROR_IMPORT_FILEPATH_INVALID);
  86.                 }
  87.             }
  88.             catch (Exception ex)
  89.             {
  90.                 MessageBox.Show(ex.Message, CONST_STRINGS.MSGBOX_HEADER_ERROR);
  91.             }
  92.             finally
  93.             {
  94.                 updateProgressBar(new object[] { -1 });
  95.                 dataReadStopWatch.Stop();
  96.                 Application.DoEvents();
  97.                 dataReadExecutionTime = ((float)dataReadStopWatch.ElapsedMilliseconds / 1000);
  98.                 MessageBox.Show(String.Format(CONST_STRINGS.DATA_IMPORT_SUCCESSFUL, data.Rows.Count, dataReadExecutionTime), CONST_STRINGS.MSGBOX_HEADER_DI);
  99.             }
  100.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement