public static IFileReader CreateFromFileName(FileInfoBase fileInfoBase) { if (IsCsvFile(fileInfoBase.FullName)) { var reader = new CsvHelper.CsvReader(new StreamReader(fileInfoBase.OpenRead()), new CsvConfiguration { HasHeaderRecord = true, Delimiter = ";" }); return new CsvReader(reader); } if (IsExcelFile(fileInfoBase.FullName)) { var excelReader = ExcelReaderFactory.CreateOpenXmlReader(fileInfoBase.Open(FileMode.Open, FileAccess.Read)); excelReader.IsFirstRowAsColumnNames = true; return new ExcelReader(excelReader); } throw new InvalidOperationException(); }
/// <summary> /// Provides a common way for opening a file stream for exclusively deleting the file. /// </summary> private static Stream GetFileDeleteStream(FileInfoBase file) { Contract.Assert(file != null); // Open file exclusively for delete sharing only return file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); }
/// <summary> /// Extracts source directories from the csproj file. /// The expected directory info list is the csproj's directory and all project references. /// </summary> /// <param name="csprojFile"></param> /// <returns></returns> private DirectoryInfoBase[] ExtractSourceDirectories(FileInfoBase csprojFile) { // Data validation Ensure.That(() => csprojFile).IsNotNull(); Ensure.That(csprojFile.Exists, string.Format("Could not find '{0}' file", csprojFile)).IsTrue(); // Initialize the extracted directories List<DirectoryInfoBase> extractedSourceDirectories = new List<DirectoryInfoBase>(); // Add the csproj's directory DirectoryInfoBase projectDirectory = this.fileSystem.DirectoryInfo.FromDirectoryName(this.fileSystem.Path.GetDirectoryName(csprojFile.FullName)); extractedSourceDirectories.Add(projectDirectory); // Load xml document XmlDocument xmlDocument = new XmlDocument(); using (Stream stream = csprojFile.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { xmlDocument.Load(stream); } // Extract XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable); xmlNamespaceManager.AddNamespace("msbuild", "http://schemas.microsoft.com/developer/msbuild/2003"); XmlNodeList xmlNodes = xmlDocument.SelectNodes("//msbuild:ProjectReference", xmlNamespaceManager); for (int i = 0; i < xmlNodes.Count; ++i) { XmlNode xmlNode = xmlNodes.Item(i); string includeValue = xmlNode.Attributes["Include"].Value; string combinedPath = this.fileSystem.Path.Combine(projectDirectory.FullName, includeValue); // The combinedPath can contains both forward and backslash path chunk. // In linux environment we can end up having "/..\" in the path which make the GetDirectoryName method bugging (returns empty). // For this reason we need to make sure that the combined path uses forward slashes combinedPath = combinedPath.Replace(@"\", "/"); // Add the combined path extractedSourceDirectories.Add(this.fileSystem.DirectoryInfo.FromDirectoryName(this.fileSystem.Path.GetDirectoryName(this.fileSystem.Path.GetFullPath(combinedPath)))); } // Return the extracted directories return extractedSourceDirectories.ToArray(); }