private static string GetNamespaceFromCsproj(FileInfoBase fileToSearch) { string nameSpace; using (Stream fr = fileToSearch.OpenRead()) { StreamReader sr = new StreamReader(fr); string fileContents = sr.ReadToEnd(); // Look for a <RootNamespace> tag string openingTag = "<RootNamespace>"; string closingTag = "</RootNamespace>"; int openingIndex = fileContents.IndexOf(openingTag, StringComparison.OrdinalIgnoreCase); if (openingIndex > -1) { int closingIndex = fileContents.IndexOf(closingTag, openingIndex + openingTag.Length, StringComparison.OrdinalIgnoreCase); int substringStartingIndex = openingIndex + openingTag.Length; nameSpace = fileContents.Substring(substringStartingIndex, closingIndex - substringStartingIndex); } else { nameSpace = FileSystem.Path.GetFileNameWithoutExtension(fileToSearch.Name); } } return(nameSpace); }
private XDocument ReadResultsFile(FileInfoBase testResultsFile) { XDocument document; using (var stream = testResultsFile.OpenRead()) { using (var streamReader = new System.IO.StreamReader(stream)) { string content = streamReader.ReadToEnd(); int begin = content.IndexOf("<!-- Pickles Begin", StringComparison.Ordinal); content = content.Substring(begin); content = content.Replace("<!-- Pickles Begin", string.Empty); int end = content.IndexOf("Pickles End -->", System.StringComparison.Ordinal); content = content.Substring(0, end); content = content.Replace("<", "<").Replace(">", ">"); var xmlReader = XmlReader.Create(new System.IO.StringReader(content)); document = XDocument.Load(xmlReader); } } return(document); }
public void ExtractSnippet() { // Declare content const string content = @"Some Content Here"; // Mock file system IFileSystem fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { "Content.txt", new MockFileData(content) } }); // Run the extraction DefaultSnippetExtractor extractor = new DefaultSnippetExtractor(); FileInfoBase fileInfoBase = fileSystem.FileInfo.FromFileName("Content.txt"); Extension.Model.PlainTextSnippet snippet = extractor.Extract(fileInfoBase, null) as Extension.Model.PlainTextSnippet; // Load the expected file content MemoryStream memoryStream = new MemoryStream(); using (var fileReader = new StreamReader(fileInfoBase.OpenRead())) using (var fileWriter = new StreamWriter(memoryStream)) { fileWriter.Write(fileReader.ReadToEnd()); } // Assert Assert.AreEqual(content, snippet.Text); }
public static void AddFile(this ZipArchive zipArchive, FileInfoBase file, string prefix, string directoryNameInArchive) { Stream fileStream = null; try { fileStream = file.OpenRead(); } catch (Exception ex) { // tolerate if file in use. // for simplicity, any exception. //tracer.TraceError(String.Format("{0}, {1}", file.FullName, ex)); return; } try { string fileName = ForwardSlashCombine(directoryNameInArchive, file.Name); ZipArchiveEntry entry = zipArchive.CreateEntry(String.Concat(prefix, fileName), CompressionLevel.Fastest); entry.LastWriteTime = file.LastWriteTime; using (Stream zipStream = entry.Open()) { fileStream.CopyTo(zipStream); } } finally { fileStream.Dispose(); } }
public static Task <string> ReadStringToEndAsync(this FileInfoBase fileInfo) { using (var reader = new StreamReader(fileInfo.OpenRead())) { reader.BaseStream.Seek(0, SeekOrigin.Begin); return(reader.ReadToEndAsync()); } }
public static string ComputeSha1(this FileInfoBase file) { using (var fileStream = file.OpenRead()) { var sha1 = new SHA1Managed(); return(BitConverter.ToString(sha1.ComputeHash(fileStream))); } }
public static double GetHeightToWidthRatio(FileInfoBase relativePath) { using (var stream = relativePath.OpenRead()) { var image = Image.FromStream(stream); return(((double)image.Height) / ((double)image.Width)); } }
private CsvHelper.CsvReader CreateCsvReader() { return(new CsvHelper.CsvReader(new StreamReader(_fileInfo.OpenRead()), new CsvConfiguration { HasHeaderRecord = true, Delimiter = ";" })); }
public static void AddFile(this ZipArchive zipArchive, FileInfoBase file, string directoryNameInArchive) { string fileName = Path.Combine(directoryNameInArchive, file.Name); ZipArchiveEntry entry = zipArchive.CreateEntry(fileName, CompressionLevel.Fastest); using (Stream zipStream = entry.Open(), fileStream = file.OpenRead()) { fileStream.CopyTo(zipStream); } }
public TItem Load(FileInfoBase fileInfo) { TItem document; using (var stream = fileInfo.OpenRead()) { document = this.Load(stream); } return(document); }
private XDocument ReadResultsFile(FileInfoBase testResultFile) { XDocument document; using (var stream = testResultFile.OpenRead()) { XmlReader xmlReader = XmlReader.Create(stream); document = XDocument.Load(xmlReader); stream.Close(); } return(document); }
public XDocument Load(FileInfoBase fileInfo) { XDocument document; using (var stream = fileInfo.OpenRead()) { XmlReader xmlReader = XmlReader.Create(stream); document = XDocument.Load(xmlReader); } return(document); }
private List <Feature> ReadResultsFile(FileInfoBase testResultsFile) { List <Feature> result; using (var stream = testResultsFile.OpenRead()) { using (var reader = new StreamReader(stream)) { result = JsonConvert.DeserializeObject <List <Feature> >(reader.ReadToEnd()); } } return(result); }
public static async Task <ZipArchiveEntry> AddFile(this ZipArchive zipArchive, FileInfoBase file, string directoryNameInArchive) { using (var fileStream = file.OpenRead()) { string fileName = ForwardSlashCombine(directoryNameInArchive, file.Name); ZipArchiveEntry entry = zipArchive.CreateEntry(fileName, CompressionLevel.Fastest); entry.LastWriteTime = file.LastWriteTime; using (var zipStream = entry.Open()) { await fileStream.CopyToAsync(zipStream); } return(entry); } }
public IDictionary <int, string> GetTabNamesByIndex() { var dictionary = new Dictionary <int, string>(); int index = 0; using (var excelReader = ExcelReaderFactory.CreateOpenXmlReader(_fileInfo.OpenRead())) { foreach (DataTable table in excelReader.AsDataSet().Tables) { dictionary.Add(index++, table.TableName); } } return(dictionary); }
private static string HashDelivery(FileInfoBase file) { using (var fs = file.OpenRead()) using (var bs = new BufferedStream(fs)) { using (var sha1 = new SHA1Managed()) { var hash = sha1.ComputeHash(bs); var formatted = new StringBuilder(2 * hash.Length); foreach (var b in hash) { formatted.AppendFormat("{0:X2}", b); } return(formatted.ToString()); } } }
public void MockFileInfo_OpenRead_ShouldReturnByteContentOfFile() { // Arrange MockFileSystem fileSystem = new MockFileSystem(); fileSystem.AddFile(XFS.Path(@"c:\temp\file.txt"), new MockFileData(new byte[] { 1, 2 })); FileInfoBase fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); // Act byte[] result = new byte[2]; using (Stream stream = fileInfo.OpenRead()) { stream.Read(result, 0, 2); } Assert.Equal(new byte[] { 1, 2 }, result); }
/// <summary> /// Loads a file from the file name. /// </summary> /// <param name="fileInfo">The file info.</param> /// <returns>The file's content.</returns> protected string LoadFile(FileInfoBase fileInfo) { // Data validation if (null == fileInfo) { throw new ArgumentNullException("fileInfo"); } // Load the file content MemoryStream memoryStream = new MemoryStream(); using (StreamReader streamReader = new StreamReader(fileInfo.OpenRead())) using (var fileWriter = new StreamWriter(memoryStream)) { fileWriter.Write(streamReader.ReadToEnd()); } // Read the code snippet from the file return(Encoding.UTF8.GetString(memoryStream.ToArray())); }
/// <summary> /// Retrieves the build script contained in the given file. /// </summary> /// <param name="file">The file containing a build script</param> /// <returns>A build script.</returns> protected async Task <ScriptFile> GetScriptAsync(FileInfoBase file) { // Parse file name and path var typeName = file.Directory.Name; var schemaName = file.Directory.Parent.Name; var fileName = Path.GetFileNameWithoutExtension(file.FullName); if (!_objectTypes.ContainsKey(typeName)) { return(new ScriptFile(new DatabaseObject(ServerName, DatabaseName, schemaName.TrimObjectName(), fileName.TrimObjectName()), new UnexpectedObjectTypeError(typeName))); } var objectType = _objectTypes[typeName]; var dbObject = new TypedDatabaseObject(ServerName, DatabaseName, schemaName.TrimObjectName(), fileName.TrimObjectName(), objectType); // Read file contents string content; try { using (var fileInputStream = file.OpenRead()) { using (var fileReader = new StreamReader(fileInputStream)) { content = await fileReader.ReadToEndAsync(); } } } catch (DirectoryNotFoundException) { return(new ScriptFile(dbObject, GetDropScript(dbObject), _sqlParser)); } catch (FileNotFoundException) { return(new ScriptFile(dbObject, GetDropScript(dbObject), _sqlParser)); } // Parse script file var script = new ScriptFile(dbObject, content, _sqlParser); return(script); }
private static string GetUserSecretsIdFromProject(FileInfoBase fileToSearch) { string openingTag = "<UserSecretsId>"; string closingTag = "</UserSecretsId>"; string userSecretsId = string.Empty; using (Stream fr = fileToSearch.OpenRead()) { StreamReader sr = new StreamReader(fr); string fileContents = sr.ReadToEnd(); int openingIndex = fileContents.IndexOf(openingTag, StringComparison.OrdinalIgnoreCase); if (openingIndex > -1) { int closingIndex = fileContents.IndexOf(closingTag, openingIndex + openingTag.Length, StringComparison.OrdinalIgnoreCase); int substringStartingIndex = openingIndex + openingTag.Length; userSecretsId = fileContents.Substring(substringStartingIndex, closingIndex - substringStartingIndex); } } return(userSecretsId); }
public Stream OpenRead() { return(_file.OpenRead()); }