Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / xsp / System / Web / Util / FileEnumerator.cs / 1 / FileEnumerator.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
* FileEnumerator class
*
* Copyright (c) 2003 Microsoft Corporation
*
* Class to efficiently enumerate the files in a directory. The only thing the framework provides
* to do this is Directory.GetFiles(), which is unusable on large directories because it returns an
* array containing all the file names at once (huge memory allocation).
*
* An efficient alternative is to use FindFirstFile/FindNextFile, which works but requires a lot
* more code. Also, it makes our code base harder to port to non-windows platforms.
*
* This FileEnumerator class solves both problem, by providing a simple and efficient wrapper.
* By working with a single object, it is almost as efficient as calling FindFirstFile/FindNextFile,
* but is much easier to use. e.g. instead of:
*
* UnsafeNativeMethods.WIN32_FIND_DATA wfd;
* IntPtr hFindFile = UnsafeNativeMethods.FindFirstFile(physicalDir + @"\*.*", out wfd);
*
* if (hFindFile == INVALID_HANDLE_VALUE)
* return;
*
* try {
* for (bool more=true; more; more=UnsafeNativeMethods.FindNextFile(hFindFile, out wfd)) {
*
* // Skip false directories
* if (wfd.cFileName == "." || wfd.cFileName == "..")
* continue;
*
* string fullPath = Path.Combine(physicalDir, wfd.cFileName);
*
* ProcessFile(fullPath);
* }
* }
* finally {
* UnsafeNativeMethods.FindClose(hFindFile);
* }
*
* we can simply write
*
* foreach (FileData fileData in FileEnumerator.Create(physicalDir)) {
* ProcessFile(fileData.FullName);
* }
*/
namespace System.Web.Util {
using System.IO;
using System.Collections;
/*
* This is a somewhat artificial base class for FileEnumerator. The main reason
* for it is to allow user code to be more readable, by looking like:
* foreach (FileData fileData in FileEnumerator.Create(path)) { ... }
* instead of
* foreach (FileEnumerator fileData in FileEnumerator.Create(path)) { ... }
*/
internal abstract class FileData {
protected string _path;
protected UnsafeNativeMethods.WIN32_FIND_DATA _wfd;
internal string Name {
get { return _wfd.cFileName; }
}
internal string FullName {
get { return _path + @"\" + _wfd.cFileName; }
}
internal bool IsDirectory {
get { return (_wfd.dwFileAttributes & UnsafeNativeMethods.FILE_ATTRIBUTE_DIRECTORY) != 0; }
}
internal bool IsHidden {
get { return (_wfd.dwFileAttributes & UnsafeNativeMethods.FILE_ATTRIBUTE_HIDDEN) != 0; }
}
internal FindFileData GetFindFileData() {
return new FindFileData(ref _wfd);
}
}
internal class FileEnumerator: FileData, IEnumerable, IEnumerator, IDisposable {
private IntPtr _hFindFile = UnsafeNativeMethods.INVALID_HANDLE_VALUE;
internal static FileEnumerator Create(string path) {
return new FileEnumerator(path);
}
private FileEnumerator(string path) {
_path = Path.GetFullPath(path);
}
~FileEnumerator() {
((IDisposable)this).Dispose();
}
// Should the current file be excluded from the enumeration
private bool SkipCurrent() {
// Skip false directories
if (_wfd.cFileName == "." || _wfd.cFileName == "..")
return true;
return false;
}
// We just return ourselves for the enumerator, to avoid creating a new object
IEnumerator IEnumerable.GetEnumerator() {
return this;
}
bool IEnumerator.MoveNext() {
for (;;) {
if (_hFindFile == UnsafeNativeMethods.INVALID_HANDLE_VALUE) {
_hFindFile = UnsafeNativeMethods.FindFirstFile(_path + @"\*.*", out _wfd);
// Empty enumeration case
if (_hFindFile == UnsafeNativeMethods.INVALID_HANDLE_VALUE)
return false;
}
else {
bool hasMoreFiles = UnsafeNativeMethods.FindNextFile(_hFindFile, out _wfd);
if (!hasMoreFiles)
return false;
}
if (!SkipCurrent())
return true;
}
}
// The current object of the enumeration is always ourselves. No new object created.
object IEnumerator.Current {
get { return this; }
}
void IEnumerator.Reset() {
// We don't support reset, though it would be easy to add if needed
throw new InvalidOperationException();
}
void IDisposable.Dispose() {
if (_hFindFile != UnsafeNativeMethods.INVALID_HANDLE_VALUE) {
UnsafeNativeMethods.FindClose(_hFindFile);
_hFindFile = UnsafeNativeMethods.INVALID_HANDLE_VALUE;
}
System.GC.SuppressFinalize(this);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
* FileEnumerator class
*
* Copyright (c) 2003 Microsoft Corporation
*
* Class to efficiently enumerate the files in a directory. The only thing the framework provides
* to do this is Directory.GetFiles(), which is unusable on large directories because it returns an
* array containing all the file names at once (huge memory allocation).
*
* An efficient alternative is to use FindFirstFile/FindNextFile, which works but requires a lot
* more code. Also, it makes our code base harder to port to non-windows platforms.
*
* This FileEnumerator class solves both problem, by providing a simple and efficient wrapper.
* By working with a single object, it is almost as efficient as calling FindFirstFile/FindNextFile,
* but is much easier to use. e.g. instead of:
*
* UnsafeNativeMethods.WIN32_FIND_DATA wfd;
* IntPtr hFindFile = UnsafeNativeMethods.FindFirstFile(physicalDir + @"\*.*", out wfd);
*
* if (hFindFile == INVALID_HANDLE_VALUE)
* return;
*
* try {
* for (bool more=true; more; more=UnsafeNativeMethods.FindNextFile(hFindFile, out wfd)) {
*
* // Skip false directories
* if (wfd.cFileName == "." || wfd.cFileName == "..")
* continue;
*
* string fullPath = Path.Combine(physicalDir, wfd.cFileName);
*
* ProcessFile(fullPath);
* }
* }
* finally {
* UnsafeNativeMethods.FindClose(hFindFile);
* }
*
* we can simply write
*
* foreach (FileData fileData in FileEnumerator.Create(physicalDir)) {
* ProcessFile(fileData.FullName);
* }
*/
namespace System.Web.Util {
using System.IO;
using System.Collections;
/*
* This is a somewhat artificial base class for FileEnumerator. The main reason
* for it is to allow user code to be more readable, by looking like:
* foreach (FileData fileData in FileEnumerator.Create(path)) { ... }
* instead of
* foreach (FileEnumerator fileData in FileEnumerator.Create(path)) { ... }
*/
internal abstract class FileData {
protected string _path;
protected UnsafeNativeMethods.WIN32_FIND_DATA _wfd;
internal string Name {
get { return _wfd.cFileName; }
}
internal string FullName {
get { return _path + @"\" + _wfd.cFileName; }
}
internal bool IsDirectory {
get { return (_wfd.dwFileAttributes & UnsafeNativeMethods.FILE_ATTRIBUTE_DIRECTORY) != 0; }
}
internal bool IsHidden {
get { return (_wfd.dwFileAttributes & UnsafeNativeMethods.FILE_ATTRIBUTE_HIDDEN) != 0; }
}
internal FindFileData GetFindFileData() {
return new FindFileData(ref _wfd);
}
}
internal class FileEnumerator: FileData, IEnumerable, IEnumerator, IDisposable {
private IntPtr _hFindFile = UnsafeNativeMethods.INVALID_HANDLE_VALUE;
internal static FileEnumerator Create(string path) {
return new FileEnumerator(path);
}
private FileEnumerator(string path) {
_path = Path.GetFullPath(path);
}
~FileEnumerator() {
((IDisposable)this).Dispose();
}
// Should the current file be excluded from the enumeration
private bool SkipCurrent() {
// Skip false directories
if (_wfd.cFileName == "." || _wfd.cFileName == "..")
return true;
return false;
}
// We just return ourselves for the enumerator, to avoid creating a new object
IEnumerator IEnumerable.GetEnumerator() {
return this;
}
bool IEnumerator.MoveNext() {
for (;;) {
if (_hFindFile == UnsafeNativeMethods.INVALID_HANDLE_VALUE) {
_hFindFile = UnsafeNativeMethods.FindFirstFile(_path + @"\*.*", out _wfd);
// Empty enumeration case
if (_hFindFile == UnsafeNativeMethods.INVALID_HANDLE_VALUE)
return false;
}
else {
bool hasMoreFiles = UnsafeNativeMethods.FindNextFile(_hFindFile, out _wfd);
if (!hasMoreFiles)
return false;
}
if (!SkipCurrent())
return true;
}
}
// The current object of the enumeration is always ourselves. No new object created.
object IEnumerator.Current {
get { return this; }
}
void IEnumerator.Reset() {
// We don't support reset, though it would be easy to add if needed
throw new InvalidOperationException();
}
void IDisposable.Dispose() {
if (_hFindFile != UnsafeNativeMethods.INVALID_HANDLE_VALUE) {
UnsafeNativeMethods.FindClose(_hFindFile);
_hFindFile = UnsafeNativeMethods.INVALID_HANDLE_VALUE;
}
System.GC.SuppressFinalize(this);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- CategoryNameCollection.cs
- CodeMemberEvent.cs
- Connector.xaml.cs
- StringHandle.cs
- HierarchicalDataBoundControl.cs
- WebBrowserSiteBase.cs
- UpdateTracker.cs
- InkSerializer.cs
- KnownBoxes.cs
- ServerValidateEventArgs.cs
- IISUnsafeMethods.cs
- SettingsPropertyCollection.cs
- ProjectionCamera.cs
- XmlNamespaceMappingCollection.cs
- DataColumnMapping.cs
- XmlSerializerObjectSerializer.cs
- DllHostInitializer.cs
- ModifierKeysConverter.cs
- Point3DCollection.cs
- DataGridViewCellContextMenuStripNeededEventArgs.cs
- COM2ExtendedUITypeEditor.cs
- UniqueIdentifierService.cs
- ValidationError.cs
- CodeAttachEventStatement.cs
- AppDomainShutdownMonitor.cs
- FlowDocumentPaginator.cs
- AspNetSynchronizationContext.cs
- WindowsNonControl.cs
- TypeNameConverter.cs
- WhitespaceSignificantCollectionAttribute.cs
- ParseElement.cs
- HealthMonitoringSection.cs
- SqlError.cs
- SymbolTable.cs
- EntityProviderServices.cs
- ImageField.cs
- DbConnectionClosed.cs
- ResXDataNode.cs
- X509CertificateCollection.cs
- WebWorkflowRole.cs
- EnumConverter.cs
- IsolatedStoragePermission.cs
- CompModSwitches.cs
- SelectedDatesCollection.cs
- TemplatePropertyEntry.cs
- FontStyle.cs
- WriterOutput.cs
- FramingChannels.cs
- KeyToListMap.cs
- GcSettings.cs
- WindowsFormsHost.cs
- TimeZoneNotFoundException.cs
- EmulateRecognizeCompletedEventArgs.cs
- SafeFileMapViewHandle.cs
- NativeMethods.cs
- ServiceOperationParameter.cs
- WindowShowOrOpenTracker.cs
- PreservationFileWriter.cs
- WebReferencesBuildProvider.cs
- PreProcessor.cs
- HiddenField.cs
- RegexParser.cs
- TreeIterator.cs
- PersonalizablePropertyEntry.cs
- SignedXml.cs
- Screen.cs
- XmlSerializerFactory.cs
- XmlArrayItemAttributes.cs
- ModelServiceImpl.cs
- ReaderWriterLock.cs
- DivideByZeroException.cs
- SmtpSection.cs
- SafeNativeMethodsCLR.cs
- assertwrapper.cs
- DispatcherHookEventArgs.cs
- TcpAppDomainProtocolHandler.cs
- XmlILOptimizerVisitor.cs
- PropertyEntry.cs
- WebScriptMetadataFormatter.cs
- GroupBoxAutomationPeer.cs
- SecurityKeyUsage.cs
- TypeBuilderInstantiation.cs
- ConstraintCollection.cs
- CodeGeneratorAttribute.cs
- TCPClient.cs
- StatusBar.cs
- WorkflowWebHostingModule.cs
- UxThemeWrapper.cs
- TextEffectResolver.cs
- SafeFileMappingHandle.cs
- JumpItem.cs
- DataGridViewAutoSizeColumnModeEventArgs.cs
- ExpressionsCollectionConverter.cs
- WindowsStartMenu.cs
- RenderingEventArgs.cs
- WeakReadOnlyCollection.cs
- HelpEvent.cs
- MediaTimeline.cs
- SiteMapNodeItem.cs
- RemoteWebConfigurationHostServer.cs