After I got this answer, I edited my code after that answer, but the code still seems kind of slow. Can I make any other improvements? And why does the code seem to get slower with every minute? It seems to start great and continue slow. Am I missing something here?
using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Linq;
using System.Diagnostics;
namespace Report
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string GetLine(string fileName, int line)
{
using (var sr = new StreamReader(fileName))
{
for (int i = 1; i < line; i++)
sr.ReadLine();
return sr.ReadLine();
}
}
private void button1_Click(object sender, EventArgs e)
{
Stopwatch timer = new Stopwatch();
timer.Start();
Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
excelapp.Visible = true;
_Workbook workbook = (_Workbook)(excelapp.Workbooks.Open(textBox2.Text));
_Worksheet worksheet = (_Worksheet)workbook.ActiveSheet;
DateTime dt = DateTime.Now;
foreach (string fileName in Directory.GetFiles(textBox1.Text, "*.txt"))
{
int row = 1, EmptyRow = 0;
while (Convert.ToString(worksheet.Range["A" + row].Value) != null)
{
row++;
EmptyRow = row;
}
var line2s = File.ReadLines(fileName).Skip(9).Take(1).ToArray();
string comp = line2s[0];
string compare = comp.Substring(30, comp.Length - 30);
if (compare != "Failed")
{
continue;
}
else
{
string[] lines = File.ReadAllLines(fileName);
string serial = lines[3];
string data = lines[4];
string time = lines[5];
string operat = lines[6];
string result = lines[9];
(worksheet.Cells[EmptyRow, 1] as Microsoft.Office.Interop.Excel.Range).Value2 = serial.Substring(30, serial.Length - 30);
(worksheet.Cells[EmptyRow, 2] as Microsoft.Office.Interop.Excel.Range).Value2 = data.Substring(30, data.Length - 30);
(worksheet.Cells[EmptyRow, 3] as Microsoft.Office.Interop.Excel.Range).Value2 = time.Substring(30, time.Length - 30);
(worksheet.Cells[EmptyRow, 4] as Microsoft.Office.Interop.Excel.Range).Value2 = operat.Substring(30, operat.Length - 30);
(worksheet.Cells[EmptyRow, 5] as Microsoft.Office.Interop.Excel.Range).Value2 = result.Substring(30, result.Length - 30);
foreach (string line in lines)
{
if (line.Contains("FixtureCoverResistance:"))
{
(worksheet.Cells[EmptyRow, 6] as Microsoft.Office.Interop.Excel.Range).Value2 = line.Substring(31, line.Length - 31);
}
else if (line.Contains("FwProgrammingCheck:"))
{
(worksheet.Cells[EmptyRow, 7] as Microsoft.Office.Interop.Excel.Range).Value2 = line.Substring(31, line.Length - 31);
}
else if (line.Contains("Checksum ="))
{
(worksheet.Cells[EmptyRow, 8] as Microsoft.Office.Interop.Excel.Range).Value2 = line.Substring(11, line.Length - 11);
}
else if (line.Contains("FwEepromCheck:"))
{
(worksheet.Cells[EmptyRow, 9] as Microsoft.Office.Interop.Excel.Range).Value2 = line.Substring(31, line.Length - 31);
}
else if (line.Contains("FixtureCoverResistanceAfterProg:"))
{
(worksheet.Cells[EmptyRow, 11] as Microsoft.Office.Interop.Excel.Range).Value2 = line.Substring(33, line.Length - 33);
}
}
}
}
TimeSpan ts = timer.Elapsed;
label2.Text = ts.ToString("mm\\:ss\\.ff");
timer.Stop();
}
private void button2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folderBrowserDialog1.SelectedPath;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = openFileDialog1.FileName.ToString();
}
}
}
}