本文整理汇总了C#中DirectoryInfo.EnumerateFileSystemInfos方法的典型用法代码示例。如果您正苦于以下问题:C# DirectoryInfo.EnumerateFileSystemInfos方法的具体用法?C# DirectoryInfo.EnumerateFileSystemInfos怎么用?C# DirectoryInfo.EnumerateFileSystemInfos使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DirectoryInfo
的用法示例。
在下文中一共展示了DirectoryInfo.EnumerateFileSystemInfos方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DoCreateFromDirectory
private static void DoCreateFromDirectory(String sourceDirectoryName, String destinationArchiveFileName,
CompressionLevel? compressionLevel, Boolean includeBaseDirectory,
Encoding entryNameEncoding)
{
// Rely on Path.GetFullPath for validation of sourceDirectoryName and destinationArchive
// Checking of compressionLevel is passed down to DeflateStream and the IDeflater implementation
// as it is a pluggable component that completely encapsulates the meaning of compressionLevel.
sourceDirectoryName = Path.GetFullPath(sourceDirectoryName);
destinationArchiveFileName = Path.GetFullPath(destinationArchiveFileName);
using (ZipArchive archive = Open(destinationArchiveFileName, ZipArchiveMode.Create, entryNameEncoding))
{
bool directoryIsEmpty = true;
//add files and directories
DirectoryInfo di = new DirectoryInfo(sourceDirectoryName);
string basePath = di.FullName;
if (includeBaseDirectory && di.Parent != null)
basePath = di.Parent.FullName;
// Windows' MaxPath (260) is used as an arbitrary default capacity, as it is likely
// to be greater than the length of typical entry names from the file system, even
// on non-Windows platforms. The capacity will be increased, if needed.
const int DefaultCapacity = 260;
char[] entryNameBuffer = ArrayPool<char>.Shared.Rent(DefaultCapacity);
try
{
foreach (FileSystemInfo file in di.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
{
directoryIsEmpty = false;
Int32 entryNameLength = file.FullName.Length - basePath.Length;
Debug.Assert(entryNameLength > 0);
if (file is FileInfo)
{
// Create entry for file:
String entryName = EntryFromPath(file.FullName, basePath.Length, entryNameLength, ref entryNameBuffer);
ZipFileExtensions.DoCreateEntryFromFile(archive, file.FullName, entryName, compressionLevel);
}
else
{
// Entry marking an empty dir:
DirectoryInfo possiblyEmpty = file as DirectoryInfo;
if (possiblyEmpty != null && IsDirEmpty(possiblyEmpty))
{
// FullName never returns a directory separator character on the end,
// but Zip archives require it to specify an explicit directory:
String entryName = EntryFromPath(file.FullName, basePath.Length, entryNameLength, ref entryNameBuffer, appendPathSeparator: true);
archive.CreateEntry(entryName);
}
}
} // foreach
// If no entries create an empty root directory entry:
if (includeBaseDirectory && directoryIsEmpty)
archive.CreateEntry(EntryFromPath(di.Name, 0, di.Name.Length, ref entryNameBuffer, appendPathSeparator: true));
}
finally
{
ArrayPool<char>.Shared.Return(entryNameBuffer);
}
} // using
} // DoCreateFromDirectory
示例2: IsDirEmpty
} // DoCreateFromDirectory
private static Boolean IsDirEmpty(DirectoryInfo possiblyEmptyDir)
{
foreach (FileSystemInfo fi in possiblyEmptyDir.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
return false;
return true;
}
示例3: DoCreateFromDirectory
private static void DoCreateFromDirectory(String sourceDirectoryName, String destinationArchiveFileName,
CompressionLevel? compressionLevel, Boolean includeBaseDirectory,
Encoding entryNameEncoding)
{
// Rely on Path.GetFullPath for validation of sourceDirectoryName and destinationArchive
// Checking of compressionLevel is passed down to DeflateStream and the IDeflater implementation
// as it is a pluggable component that completely encapsulates the meaning of compressionLevel.
sourceDirectoryName = Path.GetFullPath(sourceDirectoryName);
destinationArchiveFileName = Path.GetFullPath(destinationArchiveFileName);
using (ZipArchive archive = Open(destinationArchiveFileName, ZipArchiveMode.Create, entryNameEncoding))
{
bool directoryIsEmpty = true;
//add files and directories
DirectoryInfo di = new DirectoryInfo(sourceDirectoryName);
string basePath = di.FullName;
if (includeBaseDirectory && di.Parent != null)
basePath = di.Parent.FullName;
foreach (FileSystemInfo file in di.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
{
directoryIsEmpty = false;
Int32 entryNameLength = file.FullName.Length - basePath.Length;
Debug.Assert(entryNameLength > 0);
String entryName = file.FullName.Substring(basePath.Length, entryNameLength);
// Remove any leading slashes from the entry name:
entryName = entryName.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
if (file is FileInfo)
{
// Create entry for file:
ZipFileExtensions.DoCreateEntryFromFile(archive, file.FullName, entryName, compressionLevel);
}
else
{
// Entry marking an empty dir:
DirectoryInfo possiblyEmpty = file as DirectoryInfo;
if (possiblyEmpty != null && IsDirEmpty(possiblyEmpty))
{
// FullName never returns a directory separator character on the end,
// but Zip archives require it to specify an explicit directory:
archive.CreateEntry(entryName + Path.DirectorySeparatorChar);
}
}
} // foreach
// If no entries create an empty root directory entry:
if (includeBaseDirectory && directoryIsEmpty)
archive.CreateEntry(di.Name + Path.DirectorySeparatorChar);
} // using
} // DoCreateFromDirectory
示例4: TestEnumerateFileSystemInfosWithSearchRecursiveNoResults
public void TestEnumerateFileSystemInfosWithSearchRecursiveNoResults()
{
var di = new DirectoryInfo(longPathDirectory);
var randomFileName = Path.GetRandomFileName();
var newDi = di.CreateSubdirectory(randomFileName);
try
{
var fi = new FileInfo(Path.Combine(newDi.FullName, "filename"));
using (fi.Create())
{
}
try
{
Assert.AreEqual(0, di.EnumerateFileSystemInfos("gibberish", SearchOption.AllDirectories).Count());
}
finally
{
fi.Delete();
}
}
finally
{
newDi.Delete(true);
}
}
示例5: TestEnumerateFileSystemInfosWithSearchAndOptionMultipleResults
public void TestEnumerateFileSystemInfosWithSearchAndOptionMultipleResults()
{
var di = new DirectoryInfo(longPathDirectory);
var randomFileName = Path.GetRandomFileName();
var newDi = di.CreateSubdirectory(randomFileName);
try
{
var fi = new FileInfo(Path.Combine(newDi.FullName, "filename"));
using (fi.Create())
{
}
try
{
Assert.AreEqual(2, di.EnumerateFileSystemInfos("*").Count());
}
finally
{
fi.Delete();
}
}
finally
{
newDi.Delete(true);
}
}
示例6: CreateFromDirectory
public static void CreateFromDirectory (
string sourceDirectoryName,
string destinationArchiveFileName,
CompressionLevel compressionLevel,
bool includeBaseDirectory,
Encoding entryNameEncoding)
{
if (sourceDirectoryName == null)
throw new ArgumentNullException ("sourceDirectoryName");
if (destinationArchiveFileName == null)
throw new ArgumentNullException ("destinationArchiveFileName");
if (string.IsNullOrWhiteSpace (sourceDirectoryName))
throw new ArgumentException ("sourceDirectoryName");
if (string.IsNullOrWhiteSpace (destinationArchiveFileName))
throw new ArgumentException ("destinationArchiveFileName");
if (entryNameEncoding == Encoding.Unicode ||
entryNameEncoding == Encoding.UTF32 ||
entryNameEncoding == Encoding.UTF7)
throw new ArgumentException ("entryNameEncoding");
if (entryNameEncoding == null)
entryNameEncoding = Encoding.UTF8;
if (!Directory.Exists (sourceDirectoryName))
throw new DirectoryNotFoundException ("sourceDirectoryName is invalid or does not exist");
var sourceDir = new DirectoryInfo (Path.GetFullPath (sourceDirectoryName));
string fullBaseName = sourceDir.FullName;
if (includeBaseDirectory && sourceDir.Parent != null)
fullBaseName = sourceDir.Parent.FullName;
bool hasEntries = false;
char[] separators = new char[] {
Path.DirectorySeparatorChar,
Path.AltDirectorySeparatorChar
};
using (var zipFile = ZipFile.Open (destinationArchiveFileName, ZipArchiveMode.Create,
entryNameEncoding)) {
var entries = sourceDir.EnumerateFileSystemInfos ("*", SearchOption.AllDirectories);
foreach (var entry in entries) {
hasEntries = true;
int length = entry.FullName.Length - fullBaseName.Length;
string entryName = entry.FullName.Substring(fullBaseName.Length, length);
entryName = entryName.TrimStart(separators);
if (entry is FileInfo)
zipFile.CreateEntryFromFile (entry.FullName, entryName, compressionLevel);
else
zipFile.CreateEntry (entryName + Path.DirectorySeparatorChar);
}
// Create the base directory even if we had no entries
if (includeBaseDirectory && !hasEntries)
zipFile.CreateEntry(sourceDir.Name + Path.DirectorySeparatorChar);
}
}