public static List <string[]> ParseClipboardWithCommaSeparatedValueOrText() { List <string[]> clipboardData = new List <string[]>(); // get the data and set the parsing method based on the format // currently works with CSV and Text DataFormats IDataObject dataObj = System.Windows.Clipboard.GetDataObject(); if (dataObj != null) { string[] formats = dataObj.GetFormats(); if (formats.Contains(DataFormats.CommaSeparatedValue)) { string clipboardString = (string)dataObj.GetData(DataFormats.CommaSeparatedValue); { // EO: Subject to error when a CRLF is included as part of the data but it work for the moment and I will let it like it is // WARNING ! Subject to errors string[] lines = clipboardString.Split(new string[] { "\r\n" }, StringSplitOptions.None); string[] lineValues; foreach (string line in lines) { lineValues = CsvHelper.ParseLineCommaSeparated(line); if (lineValues != null) { clipboardData.Add(lineValues); } } } } else if (formats.Contains(DataFormats.Text)) { string clipboardString = (string)dataObj.GetData(DataFormats.Text); clipboardData = CsvHelper.ParseText(clipboardString); } } return(clipboardData); }
// ****************************************************************** public static LineSeparator GuessCsvSeparator(string oneLine) { List <Tuple <LineSeparator, int> > listOfLineSeparatorAndThereFirstLineSeparatedValueCount = new List <Tuple <LineSeparator, int> >(); listOfLineSeparatorAndThereFirstLineSeparatedValueCount.Add(new Tuple <LineSeparator, int>(LineSeparator.Tab, CsvHelper.ParseLineTabSeparated(oneLine).Count())); listOfLineSeparatorAndThereFirstLineSeparatedValueCount.Add(new Tuple <LineSeparator, int>(LineSeparator.Semicolon, CsvHelper.ParseLineSemicolonSeparated(oneLine).Count())); listOfLineSeparatorAndThereFirstLineSeparatedValueCount.Add(new Tuple <LineSeparator, int>(LineSeparator.Comma, CsvHelper.ParseLineCommaSeparated(oneLine).Count())); var bestBet = listOfLineSeparatorAndThereFirstLineSeparatedValueCount.MaxBy((n) => n.Item2).FirstOrDefault(); if (bestBet != null && bestBet.Item2 > 1) { return(bestBet.Item1); } return(LineSeparator.Unknown); }