Hello C#ers,
I apologize for the rudimentary question but I did not realize my issue till now. I have a large csv file some 214MB. Initially I made my program readalllines and it takes forever. I understand to fix this problem streamreader can help. However any of the tutorials I try don't seem to work. I understand this can be a simple fix, I would really appreciate any help. Here is the code and here is a snip of what my data looks like:
"Date","Time","Open","High","Low","Close","Up","Down"
11/12/2007,0832,88.38,88.38,88.38,88.38,250,250
11/12/2007,0832,87.75,87.75,87.75,87.75,100,100
11/12/2007,0834,88.00,88.00,88.00,88.00,50,50
11/12/2007,0834,88.00,88.00,88.00,88.00,50,50
11/12/2007,0834,88.00,88.00,88.00,88.00,50,50
private void button1_Click(object sender, EventArgs e)
{
string delimiter = (string)this.listBox1.SelectedItem;
//Check Delimiter type. If none selected Return
if (delimiter == null)
{
MessageBox.Show("Please Select a Delimiter", "Selection");
return;
}
double barFreq = (double)this.barFreq.Value;
if (barFreq == 0)
{
MessageBox.Show("Please Select an Interval", "Selection");
return;
}
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
String filename = openFileDialog1.FileName;
String[] lines = System.IO.File.ReadAllLines(filename);
DateTime minBarStartTime = DateTime.MinValue;
decimal minBarOpen = -1m;
decimal minBarHigh = 0m;
decimal minBarLow = decimal.MaxValue;
decimal minBarClose = 0m;
if (System.IO.File.Exists(@"c:\Bars.txt"))
if (MessageBox.Show(@"Delete c:\Bars.txt?", "delete output file?", MessageBoxButtons.YesNo) == DialogResult.Yes)
System.IO.File.Delete(@"c:\Bars.txt");
foreach (string line in lines)
{
String[] fields = line.Split(Convert.ToChar(delimiter));
//progressBar1.Maximum = lines.Length;
//progressBar1.Minimum = 0;
//progressBar1.TabIndex = 0;
//progressBar1.Value = 0;
//progressBar1.Step = 10;
//delegate void UpdateProgressDelegate();
// progressBar1.Invoke(UpdateProgressDelegate, new object[]75);
//void UpdateProgressBar(line)
//{
// this.progressBar1.Value = line;
//}
try
{
DateTime date = DateTime.Parse(fields[0]);
date = date.AddHours(double.Parse(fields[1].Substring(0, 2)));
date = date.AddMinutes(double.Parse(fields[1].Substring(2)));
decimal open = decimal.Parse(fields[2]);
decimal high = decimal.Parse(fields[3]);
decimal low = decimal.Parse(fields[4]);
decimal close = decimal.Parse(fields[5]);
if (high > minBarHigh)
minBarHigh = high;
if (low < minBarLow)
minBarLow = low;
if (minBarOpen == -1m)
minBarOpen = open;
if (date.Subtract(minBarStartTime).TotalSeconds > Convert.ToDouble(barFreq))
{
minBarClose = close;
if (minBarStartTime > DateTime.MinValue)
{
newMinBar(minBarStartTime, minBarOpen, minBarHigh, minBarLow, minBarClose);
System.IO.File.AppendAllText(@"c:\Bars.txt", minBarStartTime.ToString() + "," + minBarOpen.ToString() + "," + minBarHigh.ToString() + "," + minBarLow.ToString() + "," + minBarClose.ToString() + "\r\n");
}
minBarOpen = -1m;
minBarLow = decimal.MaxValue;
minBarStartTime = date;
}
}
catch
{
}
}
}
}
Much Appreciated