public static void WriteMergeCSVData(MergeCSVData data) { try { if (!File.Exists(PackageFileName)) { File.Create(PackageFileName).Close(); } StringBuilder sbPackageConfig = new StringBuilder(); sbPackageConfig.AppendLine("<configuration>"); sbPackageConfig.AppendLine("\t" + "<CSVFilePath1>" + data.CSVFilePath1 + "</CSVFilePath1>"); sbPackageConfig.AppendLine("\t" + "<CSVFilePath2>" + data.CSVFilePath2 + "</CSVFilePath2>"); sbPackageConfig.AppendLine("\t" + "<HeaderExists>" + data.HeaderExists + "</HeaderExists>"); sbPackageConfig.AppendLine("\t" + "<JsonFilePath>" + data.JsonFilePath + "</JsonFilePath>"); sbPackageConfig.AppendLine("</configuration>"); File.WriteAllText(PackageFileName, sbPackageConfig.ToString()); MergeLog.Loginformation("Write the configuration details are successful."); } catch (Exception e) { MergeLog.LogError(e); throw e; } }
public static MergeCSVData ReadMergeCSVData() { MergeCSVData obj = new MergeCSVData(); try { if (File.Exists(PackageFileName)) { XmlDocument configDoc = new XmlDocument(); configDoc.Load(PackageFileName); obj.CSVFilePath1 = configDoc.GetElementsByTagName("CSVFilePath1").Item(0) != null?configDoc.GetElementsByTagName("CSVFilePath1").Item(0).InnerText : string.Empty; obj.CSVFilePath2 = configDoc.GetElementsByTagName("CSVFilePath2").Item(0) != null?configDoc.GetElementsByTagName("CSVFilePath2").Item(0).InnerText : string.Empty; obj.HeaderExists = configDoc.GetElementsByTagName("HeaderExists").Item(0) != null?configDoc.GetElementsByTagName("HeaderExists").Item(0).InnerText : string.Empty; obj.JsonFilePath = configDoc.GetElementsByTagName("JsonFilePath").Item(0) != null?configDoc.GetElementsByTagName("JsonFilePath").Item(0).InnerText : string.Empty; MergeLog.Loginformation("Read the configuration values are successful."); } } catch (Exception e) { MergeLog.LogError(e); throw e; } return(obj); }
public void MergeCsvToJson(MergeCSVData mergeCSVData) { try { //Get the both csv file details var dtOne = businessFactory.csvFile.ReadCSVFile(mergeCSVData.CSVFilePath1, Convert.ToBoolean(mergeCSVData.HeaderExists)); var dtTwo = businessFactory.csvFile.ReadCSVFile(mergeCSVData.CSVFilePath2, Convert.ToBoolean(mergeCSVData.HeaderExists)); //merge the both csv file content using union var query = (from row_dtOne in dtOne.AsEnumerable() select row_dtOne).Concat(from row_dtTwo in dtTwo.AsEnumerable() select row_dtTwo); //group the columns data using fisrt column key var list = from key in query orderby key[0] group key by key[0] into keyGroup let childrenOfkeyGroup = from keylist in keyGroup select keylist select childrenOfkeyGroup; //Get the Json string vale by passing grouped linq object string jsonStr = Common.ListToJSONWithJavaScriptSerializer(list.ToList <IEnumerable <DataRow> >()); //Save the JSON string content File.WriteAllText(mergeCSVData.JsonFilePath + @"\" + JsonFilename, jsonStr); } catch (Exception e) { throw e; } }
static void Main(string[] args) { try { MergeLog.Loginformation("Main - Start"); //Create Mutex object & its related varibale to avoid running multiple instances const string appName = "SingleCSVInstanceApp"; bool createdNew; mutex = new Mutex(true, appName, out createdNew); MergeLog.Loginformation("Mutex thread created."); if (!createdNew) { //Prompt the message to enduser and close the current instance MessageBox.Show("Application is already running."); MergeLog.Loginformation("Mutex thread identified already win form instance is running."); return; } //Identify the Windows form exe is launched with/without parameters if (args.Length > 0) { //Declare client object and which will create all required object at runtime BusinessClient businessClient = new BusinessClient(); //Read the saved selection of CSV files and destination paths MergeCSVData data = PackageConfig.ReadMergeCSVData(); //Validating saved tool configuration values if (string.IsNullOrEmpty(data.CSVFilePath1.Trim()) || string.IsNullOrEmpty(data.CSVFilePath2.Trim()) || string.IsNullOrEmpty(data.HeaderExists.Trim()) || string.IsNullOrEmpty(data.JsonFilePath.Trim())) { //Prompt the validation message to enduser and close the current instance MessageBox.Show("Invalid Configuration values. Please use UI execution.", "MergeCSVTool"); MergeLog.Loginformation("Invalid Configuration values."); return; } // Call the merge method businessClient.MergeCsvToJson(data); MessageBox.Show("Given CSV files are merged successfully.", "MergeCSVTool"); } else { //Open Merge win form Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MergeCSVToJson()); } MergeLog.Loginformation("Main - End"); } catch (Exception ex) { MergeLog.LogError(ex); MessageBox.Show(ex.Message, "MergeCSVTool"); } }
private void MergeCSVToJson_Load(object sender, EventArgs e) { openCSVFileDlg.Filter = "CSV files (*.csv)|*.csv"; openCSVFileDlg.FileName = ""; MergeCSVData data = PackageConfig.ReadMergeCSVData(); txtCSVFilePath1.Text = data.CSVFilePath1; txtCSVFilePath2.Text = data.CSVFilePath2; txtJsonFolderPath.Text = data.JsonFilePath; chkHeader.Checked = (data.HeaderExists == string.Empty) ? false : Convert.ToBoolean(data.HeaderExists); }
private void CollectAndSaveConfigDetails() { try { mergeCSVData = new MergeCSVData(); mergeCSVData.CSVFilePath1 = txtCSVFilePath1.Text; mergeCSVData.CSVFilePath2 = txtCSVFilePath2.Text; mergeCSVData.JsonFilePath = txtJsonFolderPath.Text; mergeCSVData.HeaderExists = chkHeader.Checked.ToString(); PackageConfig.WriteMergeCSVData(mergeCSVData); } catch (Exception e) { MergeLog.LogError(e); throw e; } }