Hello,
Please would you advice and help to optimize the code below? I have a heap of text files in strange format and I need them to import to DB. I am writing SQL CLR
Stored Procedure. I need to open it twice using streamreader, and just better solution doesnt come to my mind.
Txts are in this format:
120611 ####User: X3115127
120611 ####Computer: G34031-00
120611 ####SN: DF350GGG
120611 Adobe AIR 2.6.0.19120 Adobe Systems Incorporated
120611 Adobe Flash Player 11 ActiveX 64-bit 11.2.202.235 Adobe Systems Incorporated
.
.
120611 ####Code: 32sss332v
Steps:
1.open Scan for lines containig #### and extract actual value and store it using stringbuilder
2.open Go through the rest of lines and replace tab separation with ;
finaly building rows like:
X3115127;G34031-00;DF350GGG;Adobe Air;2.6.0.19120;Adobe Systems Incorporated;;
etc
And code is:
class Program
{
static void Main(string[] args)
{
List<string> listLines = new List<string>();
StringBuilder sb = new StringBuilder();
string[] valuesToRemove = { "xxxx" };
string line;
bool contains;
string[] fileList = Directory.GetFiles(@"C:\Tests");
foreach (string file in fileList)
{
using (StreamReader r = File.OpenText(file))
{
do
{
line = r.ReadLine();
if ((line != null) && (line.Trim().Length != 0))
{
if (contains = line.Contains("####") == true)
{
if (contains = line.Contains("####Code:") == false)
{
string[] columnValue = line.Split(':');
string value = columnValue[1].Trim();
foreach (var item in valuesToRemove)
{
if (contains = value.Contains(item) == true)
{
sb.Append(value.Replace(item, "") + ";");
}
else
{
sb.Append(value + ";");
}
}
}
}
}
}
while (line != null);
}
using (StreamReader r = File.OpenText(file))
{
do
{
line = r.ReadLine();
if ((line != null) && (line.Trim().Length != 0))
{
if (contains = line.Contains("####") == false)
{
listLines.Add((sb.ToString() + line.Replace("\t", ";")).Trim());
}
}
}
while (line != null);
}
sb.Length = 0;
}