public static string jsonToCSV(string jsonContent, string delimiter) { StringWriter csvString = new StringWriter(); using (var csv = new CsvHelper.CsvWriter(csvString)) { //csv.Configuration.SkipEmptyRecords = true; //csv.Configuration.WillThrowOnMissingField = false; csv.Configuration.Delimiter = delimiter; using (var dt = jsonStringToTable(jsonContent)) { foreach (DataColumn column in dt.Columns) { csv.WriteField(column.ColumnName); } csv.NextRecord(); foreach (DataRow row in dt.Rows) { for (var i = 0; i < dt.Columns.Count; i++) { csv.WriteField(row[i]); } csv.NextRecord(); } } } return(csvString.ToString()); }
private void InjectCsv(string filePath, string fileName, IniParser.Model.SectionData sectionData) { //var fileWriter = File.OpenWrite(filePath + ".new"); var writer = new StreamWriter(filePath + ".new", false, Encoding.Default); var csvWriter = new CsvHelper.CsvWriter(writer); StreamReader reader = new StreamReader(filePath, Encoding.Default); var csvReader = new CsvHelper.CsvReader(reader); while (csvReader.Read()) { if (csvReader.Row == 2) { var headers = csvReader.FieldHeaders; foreach (var item in headers) { csvWriter.WriteField(item); } csvWriter.NextRecord(); } var ligne = csvReader.CurrentRecord; if (ligne != null) { foreach (var sectionKey in sectionData.Keys) { int valueIndex = ExtractCsvValueIndexFromString(sectionKey.KeyName); string key = ExtractKeyFromString(sectionKey.KeyName); if (ligne.Length > valueIndex) { if (ligne.Any(k => k == key)) { ligne[valueIndex] = sectionKey.Value.RemoveSurroundedQuotes().SanitizeQuotes().Trim(); if (this.RemoveSpecialsChars) { ligne[valueIndex] = ligne[valueIndex].DeleteAccentAndSpecialsChar().RemoveDiacritics(); } } } } foreach (var item in ligne) { csvWriter.WriteField(item); } csvWriter.NextRecord(); } } reader.Close(); reader.Dispose(); csvReader.Dispose(); writer.Close(); writer.Dispose(); // replace old file File.Delete(filePath); File.Move(filePath + ".new", filePath); }
public void ToFile(string outputFilePath) { using (var fileStream = File.OpenWrite(outputFilePath)) using (var streamWriter = new StreamWriter(fileStream)) using (var csvWriter = new CsvHelper.CsvWriter(streamWriter)) { csvWriter.Configuration.Delimiter = ";"; csvWriter.Configuration.Encoding = System.Text.Encoding.UTF8; csvWriter.Configuration.HasHeaderRecord = true; foreach (var header in Headers) { csvWriter.WriteField(header); } csvWriter.NextRecord(); foreach (var row in Data) { var rowAsDict = row as IDictionary <string, object>; foreach (var field in Headers) { object value = null; if (!rowAsDict.TryGetValue(field, out value) || value == null) { value = string.Empty; } csvWriter.WriteField(value); } csvWriter.NextRecord(); } } }
public void CsvHelperSync() { TextWriter tw = TextWriter.Null; var items = TestData.GetTestObjects(); var csv = new CsvHelper.CsvWriter(tw, new CsvConfiguration(CultureInfo.InvariantCulture)); csv.WriteField("Id"); csv.WriteField("Name"); csv.WriteField("Date"); csv.WriteField("IsActive"); for (int i = 0; i < ValueCount; i++) { csv.WriteField("Value" + i); } csv.NextRecord(); foreach (var item in items) { csv.WriteField(item.Id); csv.WriteField(item.Name); csv.WriteField(item.Date); csv.WriteField(item.IsActive); for (int i = 0; i < ValueCount; i++) { csv.WriteField(item.DataSet[i]); } csv.NextRecord(); } csv.Flush(); }
protected void btnExport_Click(object sender, EventArgs e) { var fileName = "redirects.csv".FileNamePlusDate(); var fileDirectory = FoldersHelper.GetPathAbsolut(FolderType.PriceTemp); if (!Directory.Exists(fileDirectory)) { Directory.CreateDirectory(fileDirectory); } using (var csvWriter = new CsvHelper.CsvWriter(new StreamWriter(fileDirectory + fileName), new CsvConfiguration() { Delimiter = ";", SkipEmptyRecords = false })) { foreach (var item in new[] { "RedirectFrom", "RedirectTo", "ProductArtNo" }) { csvWriter.WriteField(item); } csvWriter.NextRecord(); foreach (var redirect in RedirectSeoService.GetRedirectsSeo()) { csvWriter.WriteField(redirect.RedirectFrom); csvWriter.WriteField(redirect.RedirectTo); csvWriter.WriteField(redirect.ProductArtNo); csvWriter.NextRecord(); } } Response.Clear(); CommonHelper.WriteResponseFile(fileDirectory + fileName, fileName); Response.End(); }
public JsonResult _GetProductsInfo(MarketplaceFeed model) { // get the products's ASINs var infoFeeds = _productService.GetProductInfoFeeds(model); // create the file to save the list of product's ASIN var fileFullPath = string.Format("{0}\\Products_ASINS_{1:yyyyMMdd_HHmmss}.txt", _systemJobsRoot, DateTime.Now); // write the asins to the file using (var streamWriter = new StreamWriter(fileFullPath)) { var writer = new CsvHelper.CsvWriter(streamWriter); foreach (var info in infoFeeds) { writer.WriteField(info.EisSKU); writer.WriteField(info.ASIN); writer.WriteField(info.UPC); writer.NextRecord(); } } // create new job for the Amazon Get Info for the asins var systemJob = new SystemJobDto { JobType = JobType.AmazonGetInfo, Parameters = fileFullPath, SubmittedBy = User.Identity.Name }; _systemJobService.CreateSystemJob(systemJob); return(Json(new { Success = "Amazon Get Info has been passed to EIS System Jobs for execution." }, JsonRequestBehavior.AllowGet)); }
private static void WriteCSV(System.Data.Common.DbDataReader reader, TextWriter writer) { Action <string> logBadData = s => { Log.Logger.Warning(s); Console.Error.WriteLine("Unable to write record: {0}", s); }; using (var csv = new CsvHelper.CsvWriter(writer)) { csv.Configuration.TrimOptions = CsvHelper.Configuration.TrimOptions.None; int length = reader.FieldCount; for (var i = 0; i < length; i++) { csv.WriteField(reader.GetName(i)); } csv.NextRecord(); while (reader.Read()) { for (var i = 0; i < length; i++) { csv.WriteField(reader[i]); } csv.NextRecord(); } } }
public void Write(Data data) { using (var csvWriter = new CsvHelper.CsvWriter(new StreamWriter(_stream))) { foreach (string property in data.Description.Properties) { csvWriter.WriteField(property); } csvWriter.NextRecord(); foreach (IDataObject item in data.Items) { if (_transforms?.Length > 0) { foreach (ITransform transform in _transforms) { transform.TransformData(data.Description, item); } } foreach (string property in data.Description.Properties) { csvWriter.WriteField(item.Get <string>(property)); } csvWriter.NextRecord(); } } }
private async void FindAllBtn_Click(object sender, EventArgs e) { try { assocRuleRtb.Clear(); assocRuleDgv.Rows.Clear(); double percent = double.Parse(suppTxtb.Text); if (percent < 1) { MessageBox.Show("Value to low"); return; } double supp = ((double)((double)dataset.Count() / (double)100)) * percent; int support = (int)Math.Round(supp); double conf = double.Parse(confTxtb.Text); apriori = new Apriori(support, conf, toolStripProgressBar1); toolStripStatusLabel1.Text = "Đang xử lí"; toolStripStatusLabel1.Visible = true; assocRules = await apriori.LearnAsync(dataset); if (assocRules.Count() <= 500) { foreach (var rule in assocRules) { if (rule.Y.Count > 0) { assocRuleDgv.Rows.Add(rule.GetAssocRule(), (rule.Support / dataset.Count()).ToString("P1"), rule.Confidence.ToString("N")); assocRuleRtb.Text += rule.ToString() + "\n"; } } } else { MessageBox.Show("Quá nhiều dữ liệu" + "\nKết quả sẽ được lưu vào file"); CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(new StreamWriter(".\\Output.csv")); foreach (var rule in assocRules) { csvWriter.WriteField(rule.GetAssocRule()); csvWriter.WriteField(rule.Support); csvWriter.WriteField(rule.Confidence.ToString("N")); csvWriter.NextRecord(); } } toolStripProgressBar1.Visible = false; toolStripStatusLabel1.Visible = false; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void UpdateResultsJson() { var results = new Dictionary <string, List <eye_tracking_mouse.Statistics> >(); using (var calibrations_writer = new StreamWriter(Path.Combine(StudyResultsFolder, "calibrations.csv"))) using (var calibrations_csv = new CsvHelper.CsvWriter(calibrations_writer, System.Globalization.CultureInfo.InvariantCulture)) using (var clicks_writer = new StreamWriter(Path.Combine(StudyResultsFolder, "clicks.csv"))) using (var clicks_csv = new CsvHelper.CsvWriter(clicks_writer, System.Globalization.CultureInfo.InvariantCulture)) { clicks_csv.Configuration.HasHeaderRecord = false; calibrations_csv.Configuration.HasHeaderRecord = false; foreach (var configuration in configurations) { calibrations_csv.WriteField(configuration.name ?? "User Data"); clicks_csv.WriteField(configuration.name ?? "User Data"); } for (int i = 0; i < number_of_completed_sessions; i++) { calibrations_csv.NextRecord(); clicks_csv.NextRecord(); foreach (var configuration in configurations) { string configuration_string = configuration.name ?? "User Data"; var statistics_before = eye_tracking_mouse.Statistics.LoadFromFile( Path.Combine(GetUserDataPathBeforeSession(i), configuration_string, "statistics.json")); var statistics_after = eye_tracking_mouse.Statistics.LoadFromFile( Path.Combine(GetUserDataPathAfterSession(i), configuration_string, "statistics.json")); if (!results.ContainsKey(configuration_string)) { results.Add(configuration_string, new List <eye_tracking_mouse.Statistics>()); } results[configuration_string].Add( new eye_tracking_mouse.Statistics { calibrations = statistics_after.calibrations - statistics_before.calibrations, clicks = statistics_after.clicks - statistics_before.clicks }); calibrations_csv.WriteField((statistics_after.calibrations - statistics_before.calibrations).ToString()); clicks_csv.WriteField((statistics_after.clicks - statistics_before.clicks).ToString()); } } File.WriteAllText(Path.Combine(StudyResultsFolder, "results.json"), JsonConvert.SerializeObject(results)); } }
static void Main(string[] args) { IWebDriver driver = new PhantomJSDriver(); var url = "http://www.urbanspoon.com/r/1/1291/restaurant/Internacional-District/Seattle-Deli-Seattle"; driver.Navigate().GoToUrl(url); var name = driver.FindElements(By.CssSelector("span6>h1")).First().Text; var phone = driver.FindElements(By.CssSelector(".phone.tel")).First().Text; var rating = driver.FindElements(By.CssSelector(".avarage.digits.rating")).First().Text.Split('\r')[0]; var votes = driver.FindElements(By.CssSelector(".total")).First().Text.Split(' ')[0]; var sw = new StreamWriter(@"output.csv"); var csvWriter = new CsvHelper.CsvWriter(sw); csvWriter.WriteField(name); csvWriter.WriteField(phone); csvWriter.WriteField(rating); csvWriter.WriteField(votes); csvWriter.NextRecord(); csvWriter.WriteField(name + " Hello! "); csvWriter.WriteField(phone); csvWriter.WriteField(rating); csvWriter.WriteField(votes); csvWriter.NextRecord(); sw.Flush(); }
/// <summary> /// Exports all customers for the reseller to the specified file. /// </summary> /// <param name="defaultDomain">default domain of the reseller</param> /// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param> /// <param name="key">Key for this application in Azure Active Directory</param> /// <param name="customerMicrosoftId">Microsoft Id of the customer</param> /// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param> public static void ExportCustomers(string filename, string defaultDomain, string appId, string key, string resellerMicrosoftId) { GetTokens(defaultDomain, appId, key, resellerMicrosoftId); var adCustomers = Customer.GetAllCustomers(resellerMicrosoftId, adAuthorizationToken.AccessToken); using (StreamWriter outputFile = new StreamWriter(filename)) using (var writer = new CsvHelper.CsvWriter(outputFile)) { writer.WriteField("displayName"); writer.WriteField("defaultDomainName"); writer.WriteField("cspCustomerCid"); writer.WriteField("customerContextId"); writer.NextRecord(); foreach (AzureADContract adCustomer in adCustomers) { adCustomer.cspCustomerCid = Customer.GetCustomerCid(adCustomer.customerContextId.ToString(), resellerMicrosoftId, saAuthorizationToken.AccessToken); writer.WriteField(adCustomer.displayName); writer.WriteField(adCustomer.defaultDomainName); writer.WriteField(adCustomer.cspCustomerCid); writer.WriteField(adCustomer.customerContextId); writer.NextRecord(); } } }
public string WriteFile(string outpath, List <Transaction> inlista) { string MessageResult = "OK"; try { translista = inlista; outPath = outpath; var banklista = translista.GroupBy(x => x.Bank).ToList(); string[] arrayHeader = new string[] { "Konto", "Belopp", "Datum", "Typ" }; foreach (var bank in banklista) { using (var fileWriter = File.CreateText(outPath + bank.Key + ".txt")) { using (var csv = new CsvHelper.CsvWriter(fileWriter)) { csv.Configuration.Delimiter = ";"; foreach (string t in arrayHeader) { csv.WriteField(t); } csv.NextRecord(); foreach (var trans in translista.Where(x => x.Bank == bank.Key)) { csv.WriteField(trans.Konto); csv.WriteField(trans.BeloppformatOn ? trans.Belopp.ToString("+#;-#;0") : trans.Belopp.ToString("0")); csv.WriteField(trans.Datum.ToString("yyyy-MM-dd")); csv.WriteField(trans.Typ); csv.NextRecord(); } } } } } catch (Exception e) { MessageResult = e.Message; } return(MessageResult); }
public void Write(IEnumerable<CsvModel> models, TextWriter writer, string delimiter) { /* get all headers row */ List<string> headers = new List<string>(); headers.Add(CsvHeaders.ObjectType); headers.Add(CsvHeaders.Class); headers.Add(CsvHeaders.Source); foreach (var model in models) { foreach (string key in model.Values.Keys) if (!headers.Contains(key)) headers.Add(key); } using (var csvWriter = new CsvHelper.CsvWriter(writer, new CsvHelper.Configuration.CsvConfiguration() { QuoteAllFields = true, Delimiter = delimiter } )) { /* write the header row */ foreach (string header in headers) csvWriter.WriteField<string>(header); csvWriter.NextRecord(); /* write the content of each csv model */ foreach (var model in models) { for (int i = 0; i < headers.Count; i++) { string value = string.Empty; switch (i) { case 0: value = model.ObjectType; break; case 1: value = model.Class; break; case 2: value = string.Join(new string (new char[] {Constants.SourceFileDelimiter }), model.SourceFiles); break; default: if (!model.Values.TryGetValue(headers[i], out value)) value = string.Empty; break; } csvWriter.WriteField<string>(value); } csvWriter.NextRecord(); } } }
internal string createCsvWithStructure(IEnumerable<dynamic> data, CsvConfiguration configuration) { var guid = Guid.NewGuid().ToString(); var builder = new StringBuilder(); var writer = new CsvHelper.CsvWriter(new StringWriter(builder), configuration); foreach (var column in Structure) writer.WriteField(column.Name); writer.NextRecord(); foreach (var datum in data) { var dict = ToDictionary(guid, datum); foreach (var column in Structure) { var value = dict[column.Name]; var str = string.IsNullOrEmpty(column.Format) ? value.ToString() : value.ToString(column.Format); writer.WriteField(str); } writer.NextRecord(); } return builder.ToString(); }
public JsonResult _DeleteProducts(MarketplaceFeed model) { try { // get product's EIS SKU var productEisSkus = _productService.GetProductsEisSku(model); var fileFullPath = string.Format("{0}\\BulkDeleteProducts_{1:yyyyMMdd_HHmmss}.txt", _systemJobsRoot, DateTime.Now); using (var streamWriter = new StreamWriter(fileFullPath)) { var writer = new CsvHelper.CsvWriter(streamWriter); foreach (var eisSku in productEisSkus) { writer.WriteField(eisSku); writer.NextRecord(); } } // create new job for the Amazon Get Info for the asins var systemJob = new SystemJobDto { JobType = JobType.BulkDeleteProduct, Parameters = fileFullPath, SubmittedBy = User.Identity.Name }; _systemJobService.CreateSystemJob(systemJob); return(Json("Bulk deletion of products has been passed to EIS System Jobs for execution.", JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { Error = "Error in deleting products! - Message: " + EisHelper.GetExceptionMessage(ex) }, JsonRequestBehavior.AllowGet)); } }
private static bool writeIAMSCSV(IamsItem iamsRecord, String outFolder) { bool fError = false; try // to write the csv... { List <String> strHeaders = new List <string>(); strHeaders.Add("Child shelfmarks"); string fNameString = "IamsRecords"; string outPath = outFolder + @"\" + fNameString + ".csv"; if (!File.Exists(outPath)) // only write once per shelfmark... { using (var sr = new StreamWriter(outPath, false)) // changed from uce to utf8 { using (var csvFile = new CsvHelper.CsvWriter(sr, System.Globalization.CultureInfo.InvariantCulture)) { csvFile.Configuration.Delimiter = ","; //csvFile.Configuration.HasExcelSeparator = true; csvFile.WriteField("Shelfmark"); csvFile.WriteField("Item type"); csvFile.WriteField("Catalogue status"); csvFile.NextRecord(); // skips to next line... csvFile.WriteField(iamsRecord.ItemShelfmark); csvFile.WriteField(iamsRecord.ItemType); csvFile.WriteField(iamsRecord.CatalogueStatus); csvFile.NextRecord(); // skips to next line... foreach (var header in strHeaders) { csvFile.WriteField(header); } csvFile.NextRecord(); // skips to next line... foreach (var cShelfmark in iamsRecord.ChildRecordTitles) { csvFile.WriteField(cShelfmark); // field value csvFile.NextRecord(); } } } } } catch (Exception ex) { Console.WriteLine("Error writing IAMS CSV File: {0}", ex); fError = true; } return(!fError); }
public JsonResult _SubmiteBayProductReListing(MarketplaceFeed model) { try { // get the eBay products for the relisting var productItems = _productService.GeteBayItemFeeds(model).ToList(); var fileFullPath = string.Format("{0}\\eBayProductReListing_{1:yyyyMMdd_HHmmss}.csv", _systemJobsRoot, DateTime.Now); using (var streamWriter = new StreamWriter(fileFullPath)) { var writer = new CsvHelper.CsvWriter(streamWriter); foreach (var item in productItems) { writer.WriteField(item.EisSKU); writer.WriteField(item.ItemId); writer.NextRecord(); } } // create new job for eBay suggested categories var systemJob = new SystemJobDto { JobType = JobType.eBayProductsReListing, Parameters = fileFullPath, SupportiveParameters = model.Mode, SubmittedBy = User.Identity.Name }; var jobId = _systemJobService.CreateSystemJob(systemJob); return(Json(new { Success = "eBay Product ReListing has been passed to EIS System Jobs for execution.", JobId = jobId }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { Error = "Error in submitting eBay product relisting! - Message: " + EisHelper.GetExceptionMessage(ex) }, JsonRequestBehavior.AllowGet)); } }
public override void Write(DataStructure item) { foreach (var field in item.Values) { csvWriter.WriteField(field.Value); } csvWriter.NextRecord(); }
public void PutEmpty(CsvHelper.CsvWriter csv, string from, string to) { var fields = header.Split(";".ToCharArray()); for (int i = 0; i < fields.ToList().IndexOf(to) - fields.ToList().IndexOf(from); i++) { csv.WriteField(""); } }
/// <summary> /// /// </summary> /// <param name="path"></param> /// <param name="database"></param> public void Write(string path, Database database) { try { using (var writer = new System.IO.StringWriter()) { var configuration = new CsvHelper.Configuration.CsvConfiguration(); configuration.Delimiter = CultureInfo.CurrentCulture.TextInfo.ListSeparator; configuration.CultureInfo = CultureInfo.CurrentCulture; using (var csv = new CsvHelper.CsvWriter(writer, configuration)) { // columns csv.WriteField("Id"); foreach (var column in database.Columns) { csv.WriteField(column.Name); } csv.NextRecord(); // records foreach (var record in database.Records) { csv.WriteField(record.Id.ToString()); foreach (var value in record.Values) { csv.WriteField(value.Content); } csv.NextRecord(); } } using (var file = System.IO.File.CreateText(path)) { file.Write(writer.ToString()); } } } catch (Exception ex) { Debug.Print(ex.Message); Debug.Print(ex.StackTrace); } }
} // end sub /// <summary> /// <para xml:lang="en">Dump properties of a object to a TSV file. Output multiple objects on each rows of the TSV file.</para> /// <para xml:lang="ja">オブジェクトのプロパティをTSVファイルにダンプします。複数のオブジェクトをTSVファイルの各行に出力します。</para> /// </summary> /// <typeparam name="T"> /// <para xml:lang="en">The type of object to dump.</para> /// <para xml:lang="ja">ダンプするオブジェクトの型</para> /// </typeparam> /// <param name="target"> /// <para xml:lang="en">The collection of objects to dump.</para> /// <para xml:lang="ja">ダンプするオブジェクトのコレクション</para> /// </param> /// <param name="name"> /// <para xml:lang="en">The name which is used for a file.</para> /// <para xml:lang="ja">ファイルに使用される名前。</para> /// </param> /// <param name="format"> /// <para xml:lang="en"> /// The lambda expression which is used for choosing properties to dump. /// Set like <code>a=>new {a.A, a.B}</code> to dump A property and B property of the object. /// Set like <code>a=>a</code> to dump all properties of the object. /// </para> /// <para xml:lang="ja"> /// ダンプするプロパティを選択するラムダ式。 /// オブジェクトのAプロパティとBプロパティをダンプする場合には、<code>a=>new {a.A, a.B}</code> のように指定します。 /// 全てのプロパティをダンプする場合には<code>a=>a</code>を指定します。 /// </para> /// </param> /// <param name="writeHeader"> /// <para xml:lang="en"> /// Set true to output a header of a TSV file. The names of properties of the object are output. /// Set false and so a header is not outputted. Use the DumpTsvHeader method or the WriteTsvHeader method to output the header, separately. /// </para> /// <para xml:lang="ja"> /// TSVファイルの1行目にヘッダを出力する場合はtrueを指定します。オブジェクトのプロパティ名が出力されます。 /// falseを指定するとヘッダが出力されません。別途、DumpTsvHeaderメソッドやWriteTsvHeaderメソッドを使用してヘッダを出力してください。 /// </para> /// </param> public static void DumpTsv <T>(IEnumerable <T> target, string name, Func <T, object> format, bool writeHeader = true) { Dumper.WriteTextFile(name, ".tsv", writer => { var configuration = new CsvHelper.Configuration.Configuration { Delimiter = "\t" }; using (var tsv = new CsvHelper.CsvWriter(writer, configuration)) { var isFirst = true; foreach (var item in target) { var v = format(item); if (isFirst) { isFirst = false; if (writeHeader) { if (typeof(T) == typeof(string)) { tsv.WriteField("string"); } else { tsv.WriteHeader(v.GetType()); } // end if tsv.NextRecord(); } } // end if if (typeof(T) == typeof(string)) { tsv.WriteField(v); } else { tsv.WriteRecord(v); } // end if tsv.NextRecord(); } // next item } // end using (tsv) }); } // end sub
public HttpResponseMessage DownloadCsv(GenerateCsvModel model) { // Variables. var start = model.StartDate.HasValue ? model.StartDate.Value : SqlDateTime.MinValue.Value; var stop = model.EndDate.HasValue ? model.EndDate.Value.AddDays(1) : SqlDateTime.MaxValue.Value; // Get history items. var items = HistoryHelper.GetHistoryItems(start, stop); var config = new CsvHelper.Configuration.CsvConfiguration() { HasHeaderRecord = true, QuoteAllFields = true }; // write CSV data to memory stream using (var stream = new MemoryStream()) { using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true)) { using (var csvWriter = new CsvHelper.CsvWriter(writer, config)) { csvWriter.WriteHeader <HistoryCsvHeader>(); foreach (var item in items) { csvWriter.WriteField(item.Timestamp.ToString()); csvWriter.WriteField(item.Message); csvWriter.NextRecord(); } } } stream.Seek(0, SeekOrigin.Begin); return(CreateFileResponse( stream.ToArray(), $"site-activity_{start.ToString("yyyy-MM-dd")}_{stop.ToString("yyyy-MM-dd")}.csv", "text/csv" )); } }
public JsonResult _GeteBayBulkSuggestedCategories(MarketplaceFeed model) { try { // get the product feed for bul eBay suggested categories var productKeywordFeeds = _productService.GeteBaySuggestedCategoryFeed(model); var fileFullPath = string.Format("{0}\\BulkeBaySuggestedCategories_{1:yyyyMMdd_HHmmss}.csv", _systemJobsRoot, DateTime.Now); using (var streamWriter = new StreamWriter(fileFullPath)) { var writer = new CsvHelper.CsvWriter(streamWriter); foreach (var item in productKeywordFeeds) { writer.WriteField(item.EisSKU); writer.WriteField(item.Keyword); writer.NextRecord(); } } // create new job for eBay suggested categories var systemJob = new SystemJobDto { JobType = JobType.BulkeBaySuggestedCategories, Parameters = fileFullPath, SubmittedBy = User.Identity.Name }; var jobId = _systemJobService.CreateSystemJob(systemJob); return(Json(new { Success = "Bulk eBay get suggested categories has been passed to EIS System Jobs for execution.", JobId = jobId }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { Error = "Error in getting bulk eBay suggested categories! - Message: " + EisHelper.GetExceptionMessage(ex) }, JsonRequestBehavior.AllowGet)); } }
/// <summary> /// /// </summary> /// <param name="signatures"></param> /// <param name="filePath">The output file name</param> public void ExportRules(List <Signature> signatures, string filePath) { if (IsRunning == true) { OnExclamation("Already performing an export"); return; } IsRunning = true; new Thread(() => { try { CsvConfiguration csvConfiguration = new CsvConfiguration(); csvConfiguration.Delimiter = '\t'; using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write)) using (StreamWriter streamWriter = new StreamWriter(fileStream)) using (CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(streamWriter, csvConfiguration)) { // Write out the file headers csvWriter.WriteField("SID"); csvWriter.WriteField("Name"); csvWriter.WriteField("Priority"); csvWriter.WriteField("Count"); csvWriter.NextRecord(); foreach (Signature temp in signatures) { csvWriter.WriteField(temp.Sid); csvWriter.WriteField(temp.Name); csvWriter.WriteField(temp.Priority); csvWriter.WriteField(temp.Count); csvWriter.NextRecord(); } } OnComplete(); } catch (Exception ex) { OnError("An error occurred whilst performing the export: " + ex.Message); } finally { IsRunning = false; } }).Start(); }
public void ExportTranslationUnits(string filePath, IEnumerable <XlfTransUnit> units, string language, XlfDialect dialect) { using (var textWriter = new System.IO.StreamWriter(filePath, false, Encoding.UTF8)) { var csv = new CsvHelper.CsvWriter(textWriter); csv.Configuration.Encoding = Encoding.UTF8; csv.Configuration.HasHeaderRecord = false; if (IsCsvHeaderRequired) { WriteHeader(csv); } foreach (var t in units) { if (!string.IsNullOrEmpty(CustomIdColumn)) { csv.WriteField(CustomIdColumn); } csv.WriteField(t.GetId(dialect)); csv.WriteField(/*XmlUtil.DeNormalizeLineBreaks(*/ t.Source); csv.WriteField(/*XmlUtil.DeNormalizeLineBreaks(*/ t.Target); csv.WriteField(t.Optional.TargetState); if (this.IsLangColumnRequired) { csv.WriteField(language); } csv.NextRecord(); } } }
public static void ToCsv(DataTable dataTable, string csvFilePath, char Escape = '"', char Quote = '"', string Delimiter = ";", bool BoolToIntConvert = false, CultureInfo cultureInfo = null) { CsvHelper.Configuration.CsvConfiguration csvConf = new CsvHelper.Configuration.CsvConfiguration(cultureInfo ?? CultureInfo.CurrentCulture) { TrimOptions = CsvHelper.Configuration.TrimOptions.InsideQuotes, Escape = Escape, Quote = Quote, Delimiter = Delimiter, TypeConverterCache = null }; if (BoolToIntConvert) { csvConf.TypeConverterCache = new CsvHelper.TypeConversion.TypeConverterCache(); csvConf.TypeConverterCache.AddConverter <bool>(new CsvBooleanConverter()); } using (System.IO.StreamWriter textWriter = System.IO.File.CreateText(csvFilePath)) { using (CsvHelper.CsvWriter csv = new CsvHelper.CsvWriter(textWriter, csvConf)) { foreach (DataColumn column in dataTable.Columns) { csv.WriteField(column.ColumnName); } csv.NextRecord(); foreach (DataRow row in dataTable.Rows) { for (int i = 0; i < dataTable.Columns.Count; i++) { csv.WriteField(row[i]); } csv.NextRecord(); } csv.Flush(); } textWriter.Close(); } }
public static MemoryStream CreateZipCSVStream(DataTable dt, Dictionary <string, string> columnInfo, string message, string filename) { System.IO.MemoryStream ms = new System.IO.MemoryStream(); using (TextWriter tw = new StreamWriter(ms, Encoding.GetEncoding("gb2312"))) { using (CsvHelper.CsvWriter writer = new CsvHelper.CsvWriter(tw)) { if (!string.IsNullOrEmpty(message)) { writer.WriteField(message); writer.NextRecord(); } foreach (string key in columnInfo.Values) { writer.WriteField(key); } writer.NextRecord(); int i = 0; bool isnumberic = false; foreach (DataRow dr in dt.Rows) { foreach (string key in columnInfo.Keys) { string data = key.ToLower() == "rowid" ? (i + 1).ToString() : GetValue(dr, key, out isnumberic); Console.WriteLine(data); writer.WriteField(data); } i++; writer.NextRecord(); } writer.NextRecord(); return(CompressStream(ms, filename)); } } }
/// <summary> /// execute the <see cref="ExportCommand"/> /// </summary> private void ExecuteExportCommand() { var openSaveFileDialogService = ServiceLocator.Current.GetInstance <IOpenSaveFileDialogService>(); var result = openSaveFileDialogService.GetSaveFileDialog("Untitled", ".csv", "CSV File (.csv)|*.csv", "", 0); if (string.IsNullOrEmpty(result)) { return; } using (TextWriter writer = File.CreateText(result)) { var csv = new CsvHelper.CsvWriter(writer, CultureInfo.InvariantCulture); foreach (var logInfoRowViewModel in this.LogEventInfo) { csv.WriteField(logInfoRowViewModel.TimeStamp); csv.WriteField(logInfoRowViewModel.Logger); csv.WriteField(logInfoRowViewModel.LogLevel.Name); csv.WriteField(logInfoRowViewModel.Message); csv.NextRecord(); } } }
} // end sub /// <summary> /// <para xml:lang="en">Output table format values to a TSV file. Output multiple rows of multiple values on each rows of the TSV file.</para> /// <para xml:lang="ja">表形式の値をTSVファイルに出力します。複数の値の複数行をTSVファイルの各行に出力します。</para> /// </summary> /// <typeparam name="T"> /// <para xml:lang="en">The type of object to dump.</para> /// <para xml:lang="ja">ダンプするオブジェクトの型</para> /// </typeparam> /// <param name="target"> /// <para xml:lang="en">The collection of table format values to output.</para> /// <para xml:lang="ja">出力する表形式の値のコレクション。</para> /// </param> /// <param name="name"> /// <para xml:lang="en">The name which is used for a file.</para> /// <para xml:lang="ja">ファイルに使用される名前。</para> /// </param> /// <param name="header"> /// <para xml:lang="en"> /// Set a list of names, to output a header of TSV file. /// Set null, and so a header is not outputted. Use the DumpTsvHeader method or the WriteTsvHeader method to output the header, separately. /// </para> /// <para xml:lang="ja"> /// TSFファイルのヘッダを出力するときには、名前の一覧を指定します。 /// nullを指定するとヘッダは出力されません。別途、DumpTsvHeaderメソッドやWriteTsvHeaderメソッドを使用してヘッダを出力してください。 /// </para> /// </param> public static void WriteTsv <T>(IEnumerable <IEnumerable <T> > target, string name, IEnumerable <string> header = null) { Dumper.WriteTextFile(name, ".tsv", writer => { var configuration = new CsvHelper.Configuration.Configuration { Delimiter = "\t" }; using (var tsv = new CsvHelper.CsvWriter(writer, configuration)) { if (header != null) { foreach (var c in header) { tsv.WriteField(c); } tsv.NextRecord(); } // end if foreach (var item in target) { if (item.GetType() == typeof(string)) { tsv.WriteField(item); } else { foreach (var f in item) { tsv.WriteField(f); } } tsv.NextRecord(); } // next item } // end using (tsv) }); } // end sub
public bool writetofile(string query) { using (var textwriter = new StreamWriter(query)) { try { if (winners.Count > 0) { var writer = new CsvHelper.CsvWriter(textwriter, false); writer.Configuration.Delimiter = ","; writer.WriteComment("This draw name is " + drawname + " ." + "Draw Dated on " + DateTime.Now.ToString()); writer.NextRecord(); writer.WriteHeader <KeyValuePair <int, string> >(); writer.NextRecord(); foreach (var item in winners) { writer.WriteField(item.Key); writer.WriteField(item.Value); writer.NextRecord(); } return(true); } else { return(false); } } catch (Exception ex) { throw ex; } } }
// Write to CSV public static void addRecord(int xAcc, int yAcc, int zAcc, string filepath) { List <string> records = new List <string>() { xAcc.ToString(), yAcc.ToString(), zAcc.ToString(), "" }; using (System.IO.StreamWriter file = new System.IO.StreamWriter(@filepath, true)) { using (var csv = new CsvHelper.CsvWriter(file)) { csv.WriteField(records); csv.NextRecord(); // prepares new data to write (carriage return in CSV) } } }
/// <summary> /// /// </summary> /// <param name="dataDirectory"></param> /// <param name="outputDirectory"></param> public void PostProcess(string dataDirectory, string outputDirectory) { CsvConfiguration csvConfiguration = new CsvConfiguration(); csvConfiguration.QuoteAllFields = true; using (FileStream fileStream = new FileStream(System.IO.Path.Combine(outputDirectory, "Attachment.Hashes.csv"), FileMode.Append, FileAccess.Write, FileShare.Read)) using (StreamWriter streamWriter = new StreamWriter(fileStream)) using (CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(streamWriter, csvConfiguration)) { // Now MD5 the files foreach (string file in System.IO.Directory.EnumerateFiles(outputDirectory, "*.xml", SearchOption.AllDirectories)) { string fileName = System.IO.Path.GetFileName(file); if (fileName.StartsWith("Message.Details.") == false) { continue; } MessageDetails messageDetails = new MessageDetails(); string ret = messageDetails.Load(file); if (ret.Length == 0) { foreach (AttachmentDetails attachment in messageDetails.Attachments) { csvWriter.WriteField(attachment.Md5); csvWriter.WriteField(attachment.File); csvWriter.WriteField(messageDetails.SrcIp); csvWriter.WriteField(messageDetails.SrcPort); csvWriter.WriteField(messageDetails.DstIp); csvWriter.WriteField(messageDetails.DstPort); csvWriter.WriteField(messageDetails.To); csvWriter.WriteField(messageDetails.From); csvWriter.WriteField(messageDetails.MailFrom); csvWriter.WriteField(messageDetails.Sender); csvWriter.WriteField(messageDetails.Subject); csvWriter.WriteField(messageDetails.Date); csvWriter.NextRecord(); } } } } ProcessAttachmentHashes(outputDirectory); }
/// <summary> /// /// </summary> /// <param name="dataDirectory"></param> /// <param name="outputDirectory"></param> public void PostProcess(string dataDirectory, string outputDirectory) { CsvConfiguration csvConfiguration = new CsvConfiguration(); csvConfiguration.QuoteAllFields = true; using (FileStream fileStream = new FileStream(System.IO.Path.Combine(outputDirectory, "Urls.csv"), FileMode.Append, FileAccess.Write, FileShare.Read)) using (StreamWriter streamWriter = new StreamWriter(fileStream)) using (CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(streamWriter, csvConfiguration)) { foreach (string file in System.IO.Directory.EnumerateFiles(outputDirectory, "*.xml", SearchOption.AllDirectories)) { string fileName = System.IO.Path.GetFileName(file); if (fileName.StartsWith("Url.Details.") == false) { continue; } UrlDetails urlDetails = new UrlDetails(); string ret = urlDetails.Load(file); if (ret.Length == 0) { foreach (string url in urlDetails.Urls) { csvWriter.WriteField(url); csvWriter.WriteField(urlDetails.SrcIp); csvWriter.WriteField(urlDetails.SrcPort); csvWriter.WriteField(urlDetails.DstIp); csvWriter.WriteField(urlDetails.DstPort); csvWriter.NextRecord(); } } } } }
internal string createCsvWithoutStructure(IEnumerable<dynamic> data, CsvConfiguration configuration) { var guid = Guid.NewGuid().ToString(); var builder = new StringBuilder(); var writer = new CsvHelper.CsvWriter(new StringWriter(builder), configuration); if (data.Count() == 0) return null; var first = ToDictionary(guid, data.First()); foreach (var name in first.Keys) writer.WriteField(name); writer.NextRecord(); foreach (var datum in data) { var dict = ToDictionary(guid, datum); foreach (var key in first.Keys) writer.WriteField(dict[key].ToString()); writer.NextRecord(); } return builder.ToString(); }
/// <summary> /// /// </summary> /// <param name="events"></param> /// <param name="filePath">The output file name</param> public void ExportEventCurrent(List<Event> events, string filePath) { if (IsRunning == true) { OnExclamation("Already performing an export"); return; } IsRunning = true; new Thread(() => { try { CsvConfiguration csvConfiguration = new CsvConfiguration(); csvConfiguration.Delimiter = '\t'; using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write)) using (StreamWriter streamWriter = new StreamWriter(fileStream)) using (CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(streamWriter, csvConfiguration)) { // Write out the file headers csvWriter.WriteField("CID"); csvWriter.WriteField("Src IP"); csvWriter.WriteField("Src Port"); csvWriter.WriteField("Dst IP"); csvWriter.WriteField("Dst Port"); csvWriter.WriteField("Protocol"); csvWriter.WriteField("Timestamp"); csvWriter.WriteField("TCP Flags"); csvWriter.WriteField("Payload (ASCII)"); csvWriter.NextRecord(); foreach (Event temp in events) { csvWriter.WriteField(temp.Cid); csvWriter.WriteField(temp.IpSrcTxt); csvWriter.WriteField(temp.SrcPort); csvWriter.WriteField(temp.IpDstTxt); csvWriter.WriteField(temp.DstPort); csvWriter.WriteField(temp.Protocol); csvWriter.WriteField(temp.Timestamp); csvWriter.WriteField(temp.TcpFlagsString); csvWriter.WriteField(temp.PayloadAscii); csvWriter.NextRecord(); } } OnComplete(); } catch (Exception ex) { OnError("An error occurred whilst performing the export: " + ex.Message); } finally { IsRunning = false; } }).Start(); }
/// <summary> /// /// </summary> /// <param name="filePath"></param> /// <param name="dateFrom"></param> /// <param name="initials"></param> /// <param name="text"></param> public void ExportAcknowledgmentsFrom(string filePath, string dateFrom, string initials, bool text) { if (IsRunning == true) { OnExclamation("Already performing an export"); return; } IsRunning = true; new Thread(() => { try { if (text == true) { using (NPoco.Database db = new NPoco.Database(Db.GetOpenMySqlConnection())) using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write)) using (StreamWriter streamWriter = new StreamWriter(fileStream)) { string query = _sql.GetQuery(snorbert.Configs.Sql.Query.SQL_ACKNOWLEDGEMENT); query = query.Replace("#WHERE#", @"WHERE acknowledgment.initials = @0 AND acknowledgment.timestamp > @1"); var data = db.Fetch<Dictionary<string, object>>(query, new object[] { initials, dateFrom }); foreach (Dictionary<string, object> temp in data) { streamWriter.WriteLine("Signature: " + temp["sig_name"].ToString()); streamWriter.WriteLine("SID/GID: " + temp["sig_sid"].ToString() + "/" + temp["sig_gid"].ToString()); if (temp["successful"] == null) { streamWriter.WriteLine("Acknowledgement: " + temp["description"].ToString()); } else { streamWriter.WriteLine("Acknowledgement: " + temp["description"].ToString() + " (" + temp["successful"].ToString() + ")"); } streamWriter.WriteLine("Notes: " + temp["notes"].ToString()); streamWriter.WriteLine(string.Empty); } OnComplete(); } } else { CsvConfiguration csvConfiguration = new CsvConfiguration(); csvConfiguration.Delimiter = '\t'; using (NPoco.Database db = new NPoco.Database(Db.GetOpenMySqlConnection())) using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write)) using (StreamWriter streamWriter = new StreamWriter(fileStream)) using (CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(streamWriter, csvConfiguration)) { string query = _sql.GetQuery(snorbert.Configs.Sql.Query.SQL_ACKNOWLEDGEMENT); query = query.Replace("#WHERE#", @"WHERE acknowledgment.initials = @0 AND acknowledgment.timestamp > @1"); var data = db.Fetch<Dictionary<string, object>>(query, new object[] { initials, dateFrom }); // Write out the file headers csvWriter.WriteField("Sig Name"); csvWriter.WriteField("Sig SID"); csvWriter.WriteField("Sig GID"); csvWriter.WriteField("Acknowledgement"); csvWriter.WriteField("Notes"); csvWriter.NextRecord(); foreach (Dictionary<string, object> temp in data) { csvWriter.WriteField(temp["sig_name"].ToString()); csvWriter.WriteField(temp["sig_sid"].ToString()); csvWriter.WriteField(temp["sig_gid"].ToString()); if (temp["successful"] == null) { csvWriter.WriteField(temp["description"].ToString()); } else { csvWriter.WriteField(temp["description"].ToString() + " (" + temp["successful"].ToString() + ")"); } csvWriter.WriteField(temp["notes"].ToString()); csvWriter.NextRecord(); } OnComplete(); } } } catch (Exception ex) { OnError("An error occurred whilst performing the export: " + ex.Message); } finally { IsRunning = false; } }).Start(); }
/// <summary> /// /// </summary> /// <param name="signatures"></param> /// <param name="filePath">The output file name</param> public void ExportRules(List<Signature> signatures, string filePath) { if (IsRunning == true) { OnExclamation("Already performing an export"); return; } IsRunning = true; new Thread(() => { try { CsvConfiguration csvConfiguration = new CsvConfiguration(); csvConfiguration.Delimiter = '\t'; using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write)) using (StreamWriter streamWriter = new StreamWriter(fileStream)) using (CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(streamWriter, csvConfiguration)) { // Write out the file headers csvWriter.WriteField("SID"); csvWriter.WriteField("Name"); csvWriter.WriteField("Priority"); csvWriter.WriteField("Count"); csvWriter.NextRecord(); foreach (Signature temp in signatures) { csvWriter.WriteField(temp.Sid); csvWriter.WriteField(temp.Name); csvWriter.WriteField(temp.Priority); csvWriter.WriteField(temp.Count); csvWriter.NextRecord(); } } OnComplete(); } catch (Exception ex) { OnError("An error occurred whilst performing the export: " + ex.Message); } finally { IsRunning = false; } }).Start(); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void menuFileExportUrls_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.Description = "Select the export directory"; if (fbd.ShowDialog(this) == DialogResult.Cancel) { return; } string directory = fbd.SelectedPath; string fileName = System.IO.Path.Combine(directory, "urls.tsv"); new Thread(() => { MethodInvoker methodInvoker = delegate { using (new HourGlass(this)) { CsvConfiguration csvConfiguration = new CsvConfiguration(); csvConfiguration.Delimiter = "\t"; using (FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) using (StreamWriter streamWriter = new StreamWriter(fileStream)) using (CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(streamWriter, csvConfiguration)) { csvWriter.WriteField("Host"); csvWriter.WriteField("Destination IP"); csvWriter.WriteField("URL"); csvWriter.NextRecord(); List<string> temp = new List<string>(); foreach (Session session in listSession.Objects) { if (File.Exists(System.IO.Path.Combine(this.dataDirectory, session.Guid + ".urls")) == false) { continue; } string[] urls = File.ReadAllLines(System.IO.Path.Combine(this.dataDirectory, session.Guid + ".urls")); foreach (string url in urls) { string tempUrl = url.Trim(); if (tempUrl.Length == 0) { continue; } if (temp.Contains(session.HttpHost + tempUrl) == false) { temp.Add(session.HttpHost + tempUrl); } csvWriter.WriteField(session.HttpHost); csvWriter.WriteField(session.DstIpText); csvWriter.WriteField(tempUrl); csvWriter.NextRecord(); } } temp.Sort(); foreach (string url in temp) { IO.WriteUnicodeTextToFile(url, System.IO.Path.Combine(directory, "urls.txt"), true); } } } }; if (this.InvokeRequired == true) { this.BeginInvoke(methodInvoker); } else { methodInvoker.Invoke(); } }).Start(); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void contextExportUniqueSourceIp_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "Select the export CSV"; saveFileDialog.Filter = "TSV Files|*.tsv"; if (saveFileDialog.ShowDialog(this) == DialogResult.Cancel) { return; } List<Session> sessions = listSession.Objects.Cast<Session>().ToList(); var unique = sessions.Select(s => s.SrcIpText).Distinct(); CsvConfiguration csvConfiguration = new CsvConfiguration(); csvConfiguration.Delimiter = "\t"; using (FileStream fileStream = new FileStream(saveFileDialog.FileName, FileMode.Append, FileAccess.Write)) using (StreamWriter streamWriter = new StreamWriter(fileStream)) using (CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(streamWriter, csvConfiguration)) { csvWriter.WriteField("Source IP"); csvWriter.NextRecord(); foreach (var ip in unique) { csvWriter.WriteField(ip); csvWriter.NextRecord(); } } }
/// <summary> /// /// </summary> /// <param name="filePath">The output file name</param> /// <param name="dateFrom"></param> /// <param name="dateTo"></param> /// <param name="sid"></param> public void ExportEventsAll(string filePath, string dateFrom, string dateTo, string sid) { if (IsRunning == true) { OnExclamation("Already performing an export"); return; } IsRunning = true; new Thread(() => { try { using (NPoco.Database db = new NPoco.Database(Db.GetOpenMySqlConnection())) { string query = _sql.GetQuery(snorbert.Configs.Sql.Query.SQL_RULES_EVENTS_EXPORT); query = query.Replace("#WHERE#", @"WHERE exclude.id IS NULL AND event.timestamp > @0 AND event.timestamp < @1 AND signature.sig_sid = @2"); List<Event> events = db.Fetch<Event>(query, new object[] { dateFrom, dateTo, sid }); events = Helper.ProcessEventDataSet(events); CsvConfiguration csvConfiguration = new CsvConfiguration(); csvConfiguration.Delimiter = '\t'; using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write)) using (StreamWriter streamWriter = new StreamWriter(fileStream)) using (CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(streamWriter, csvConfiguration)) { // Write out the file headers csvWriter.WriteField("CID"); csvWriter.WriteField("Src IP"); csvWriter.WriteField("Src Port"); csvWriter.WriteField("Dst IP"); csvWriter.WriteField("Dst Port"); csvWriter.WriteField("Protocol"); csvWriter.WriteField("Timestamp"); csvWriter.WriteField("TCP Flags"); csvWriter.WriteField("Payload (ASCII)"); csvWriter.WriteField("Payload (HEX)"); csvWriter.NextRecord(); foreach (var item in events) { csvWriter.WriteField(item.Cid); csvWriter.WriteField(item.IpSrcTxt); csvWriter.WriteField(item.SrcPort); csvWriter.WriteField(item.IpDst); csvWriter.WriteField(item.DstPort); csvWriter.WriteField(item.Protocol); csvWriter.WriteField(item.Timestamp); csvWriter.WriteField(item.TcpFlagsString); csvWriter.WriteField(item.PayloadAscii); csvWriter.WriteField( Helper.ConvertByteArrayToHexString(item.PayloadHex)); csvWriter.NextRecord(); } } OnComplete(); } } catch (Exception ex) { OnError("An error occurred whilst performing the export: " + ex.Message); } finally { IsRunning = false; } }).Start(); }
/// <summary> /// /// </summary> /// <param name="filePath">The output file name</param> public void ExportExcludes(string filePath) { if (IsRunning == true) { OnExclamation("Already performing an export"); return; } IsRunning = true; new Thread(() => { try { List<Exclude> excludes = new List<Exclude>(); using (NPoco.Database dbMySql = new NPoco.Database(Db.GetOpenMySqlConnection())) { var data = dbMySql.Fetch<Dictionary<string, object>>(_sql.GetQuery(snorbert.Configs.Sql.Query.SQL_EXCLUDES)); foreach (Dictionary<string, object> temp in data) { Exclude exclude = new Exclude(); exclude.Id = long.Parse(temp["id"].ToString()); exclude.SigId = long.Parse(temp["sig_id"].ToString()); exclude.SigSid = long.Parse(temp["sig_sid"].ToString()); exclude.Rule = temp["sig_name"].ToString(); exclude.SourceIpText = temp["ip_src"].ToString(); exclude.DestinationIpText = temp["ip_dst"].ToString(); if (((byte[])temp["fp"])[0] == 48) { exclude.FalsePositive = false; } else { exclude.FalsePositive = true; } exclude.Timestamp = DateTime.Parse(temp["timestamp"].ToString()); excludes.Add(exclude); } } CsvConfiguration csvConfiguration = new CsvConfiguration(); csvConfiguration.Delimiter = '\t'; using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write)) using (StreamWriter streamWriter = new StreamWriter(fileStream)) using (CsvHelper.CsvWriter csvWriter = new CsvHelper.CsvWriter(streamWriter, csvConfiguration)) { // Write out the file headers csvWriter.WriteField("Sig. ID"); csvWriter.WriteField("Source IP"); csvWriter.WriteField("Destination IP"); csvWriter.WriteField("FP"); csvWriter.WriteField("Comment"); csvWriter.WriteField("Sig. Name"); csvWriter.WriteField("Timestamp"); csvWriter.WriteField("Sig."); csvWriter.NextRecord(); foreach (var temp in excludes) { csvWriter.WriteField(temp.SigId); csvWriter.WriteField(temp.SourceIpText); csvWriter.WriteField(temp.DestinationIpText); csvWriter.WriteField(temp.FalsePositive); csvWriter.WriteField(temp.Comment); csvWriter.WriteField(temp.Rule); csvWriter.WriteField(temp.Timestamp); csvWriter.WriteField(temp.Rule); csvWriter.NextRecord(); } } OnComplete(); } catch (Exception ex) { OnError("An error occurred whilst performing the export: " + ex.Message); } finally { IsRunning = false; } }).Start(); }