本文整理汇总了C#中System.IO.Abstractions.FileInfoBase.Open方法的典型用法代码示例。如果您正苦于以下问题:C# FileInfoBase.Open方法的具体用法?C# FileInfoBase.Open怎么用?C# FileInfoBase.Open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Abstractions.FileInfoBase
的用法示例。
在下文中一共展示了FileInfoBase.Open方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateFromFileName
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();
}
示例2: GetFileDeleteStream
/// <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);
}
示例3: ExtractSourceDirectories
/// <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();
}