/// <summary> /// Creates a new CSV writer using the given <see cref="StreamWriter"/> /// and <see cref="CsvWriterOptions"/>. /// </summary> /// <param name="writer">The <see cref="StreamWriter"/> use to write the CSV file.</param> /// <param name="options">The <see cref="CsvWriterOptions"/> used to write the CSV file.</param> public CsvWriter(TextWriter writer, CsvWriterOptions options) { this.writer = writer; Delimiter = options.Delimiter; Quote = options.Quote; HasHeaderRecord = options.HasHeaderRecord; PropertyBindingFlags = options.PropertyBindingFlags; }
/// <summary> /// /// </summary> /// <param name="options"></param> /// <returns></returns> private static CsvWriterOptions SetCsvWriterConfig(Options options) { CsvHelper.CsvWriterOptions csvWriterOptions = new CsvWriterOptions(); try { switch (options.Delimiter) { case "'\\t'": csvWriterOptions.Delimiter = '\t'; break; case "\\t": csvWriterOptions.Delimiter = '\t'; break; default: csvWriterOptions.Delimiter = char.Parse(options.Delimiter); break; } } catch (Exception ex) { Console.WriteLine("Unable to set delimiter. Defaulting to comma \",\": " + ex.Message); csvWriterOptions.Delimiter = ','; } return csvWriterOptions; }
/// <summary> /// /// </summary> /// <param name="options"></param> /// <param name="hits"></param> private static void PrintHits(Options options, List <Hit> hits) { try { CsvHelper.CsvWriterOptions csvWriterOptions = SetCsvWriterConfig(options); using (MemoryStream memoryStream = new MemoryStream()) using (StreamWriter streamWriter = new StreamWriter(memoryStream)) using (CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(streamWriter, csvWriterOptions)) { // Write out the file headers csvWriter.WriteField("Last Modified"); csvWriter.WriteField("Last Update"); csvWriter.WriteField("Path"); csvWriter.WriteField("File Size"); csvWriter.WriteField("Process Exec Flag"); csvWriter.NextRecord(); var sorted = GetSortedHits(options, hits); foreach (Hit hit in sorted) { switch (hit.Type) { case Global.CacheType.CacheEntryXp: // Windows XP Shim Cache csvWriter.WriteField(hit.LastModified.ToShortDateString() + " " + hit.LastModified.ToShortTimeString()); csvWriter.WriteField(hit.LastUpdate.ToShortDateString() + " " + hit.LastUpdate.ToShortTimeString()); csvWriter.WriteField(hit.Path); csvWriter.WriteField(hit.FileSize.ToString()); csvWriter.WriteField(hit.ProcessExecFlag); csvWriter.NextRecord(); break; case Global.CacheType.CacheEntryNt5: // Windows 2k3/Vista/2k8 Shim Cache case Global.CacheType.CacheEntryNt6: // Windows 7/2k8-R2 Shim Cache csvWriter.WriteField(hit.LastModified.ToShortDateString() + " " + hit.LastModified.ToShortTimeString()); csvWriter.WriteField("N/A"); csvWriter.WriteField(hit.Path); csvWriter.WriteField("N/A"); csvWriter.WriteField(hit.ProcessExecFlag); csvWriter.NextRecord(); break; } } string output = string.Empty; memoryStream.Position = 0; using (StreamReader streamReader = new StreamReader(memoryStream)) { output = streamReader.ReadToEnd(); } Console.Write(output); if (options.Output.Length > 0) { string ret = IO.WriteUnicodeTextToFile(output, options.Output, false); if (ret.Length > 0) { } } } } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } }