/// <summary> /// Read the settings from a custom source /// </summary> /// <param name="dataSource">The settings data source.</param> /// <param name="context">The preprocessing context.</param> /// <returns></returns> public DataTable ReadSettings(DataSource dataSource, PreprocessingContext context) { string fileName = null; string arguments = null; ParseArguments(dataSource.Path, out fileName, out arguments); DataTable dt = null; string tempFile = Path.GetTempFileName(); try { arguments = arguments.Replace("@tempFile@", tempFile); using (Process customProcess = new Process()) { customProcess.StartInfo.FileName = fileName; customProcess.StartInfo.Arguments = arguments; customProcess.StartInfo.UseShellExecute = false; customProcess.StartInfo.RedirectStandardOutput = true; customProcess.StartInfo.RedirectStandardError = false; customProcess.StartInfo.CreateNoWindow = true; customProcess.Start(); Console.WriteLine(customProcess.StandardOutput.ReadToEnd()); customProcess.WaitForExit(); } IConfigSettingsReader reader = new CsvSpreadsheetFileReader(); DataSource ds = new DataSource(tempFile, DataSourceType.Spreadsheet, DataSourceSpreadsheetFormat.Csv); dt = reader.ReadSettings(ds, context); } finally { if (File.Exists(tempFile)) { File.Delete(tempFile); } } return(dt); }
/// <summary> /// Read the contents of the Settings worksheet in the specified Excel file into a DataTable. /// Excel files up to Excel 2000 and XML Spreadsheet 2003 files are both supported. /// </summary> /// <param name="dataSource">Path to the source (file or db connection)</param> public DataTable LoadDataTableFromDataSource(DataSource dataSource) { IConfigSettingsReader reader = null; switch (dataSource.SourceType) { case DataSourceType.Spreadsheet: switch (dataSource.SpreadsheetFormat) { case DataSourceSpreadsheetFormat.Xls: // Excel binary format reader = new BinarySpreadsheetFileReader(); break; case DataSourceSpreadsheetFormat.Csv: // CSV format reader = new CsvSpreadsheetFileReader(); break; case DataSourceSpreadsheetFormat.Xml: // XML Spreadsheet 2003 format reader = new XmlSpreadsheetFileReader(); break; default: throw new ArgumentException(string.Format("Spreadsheet file type not supported: {0}", dataSource)); } break; case DataSourceType.Database: reader = new SqlDatabaseReader(); break; case DataSourceType.Custom: reader = new CustomReader(); break; } if (!dataSource.Exists) { throw new FileNotFoundException("The specified input file " + dataSource.Path + " does not exist.", dataSource.Path); } DataTable dt = reader.ReadSettings(dataSource, Context); // some formats such as CSV are going to come in with env names in row 0 and first values in row 1 // adjust them to look like the excel spreadsheets by putting in empty rows if (dataSource.RequiresRowFixup) { // Add rows so that environment names are found in _environmentNameRowIndex for (int i = 1; i < Context.EnvironmentNameRowIndex; i++) { dt.Rows.InsertAt(dt.NewRow(), 0); } // Add rows so that first values are found in _firstValueRowIndex for (int i = Context.EnvironmentNameRowIndex + 1; i < Context.FirstValueRowIndex; i++) { dt.Rows.InsertAt(dt.NewRow(), Context.EnvironmentNameRowIndex); } } return(dt); }