Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Configuration / System / Configuration / FileUtil.cs / 1305376 / FileUtil.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Configuration { using System; using System.IO; using Microsoft.Win32; using System.Runtime.InteropServices; static internal class FileUtil { const int HRESULT_WIN32_FILE_NOT_FOUND = unchecked((int)0x80070002); const int HRESULT_WIN32_PATH_NOT_FOUND = unchecked((int)0x80070003); // // Use to avoid the perf hit of a Demand when the Demand is not necessary for security. // // If trueOnError is set, then return true if we cannot confirm that the file does NOT exist. // internal static bool FileExists(string filename, bool trueOnError) { UnsafeNativeMethods.WIN32_FILE_ATTRIBUTE_DATA data; bool ok = UnsafeNativeMethods.GetFileAttributesEx(filename, UnsafeNativeMethods.GetFileExInfoStandard, out data); if (ok) { // The path exists. Return true if it is a file, false if a directory. return (data.fileAttributes & (int) FileAttributes.Directory) != (int) FileAttributes.Directory; } else { if (!trueOnError) { return false; } else { // Return true if we cannot confirm that the file does NOT exist. int hr = Marshal.GetHRForLastWin32Error(); if (hr == HRESULT_WIN32_FILE_NOT_FOUND || hr == HRESULT_WIN32_PATH_NOT_FOUND) { return false; } else { return true; } } } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Configuration { using System; using System.IO; using Microsoft.Win32; using System.Runtime.InteropServices; static internal class FileUtil { const int HRESULT_WIN32_FILE_NOT_FOUND = unchecked((int)0x80070002); const int HRESULT_WIN32_PATH_NOT_FOUND = unchecked((int)0x80070003); // // Use to avoid the perf hit of a Demand when the Demand is not necessary for security. // // If trueOnError is set, then return true if we cannot confirm that the file does NOT exist. // internal static bool FileExists(string filename, bool trueOnError) { UnsafeNativeMethods.WIN32_FILE_ATTRIBUTE_DATA data; bool ok = UnsafeNativeMethods.GetFileAttributesEx(filename, UnsafeNativeMethods.GetFileExInfoStandard, out data); if (ok) { // The path exists. Return true if it is a file, false if a directory. return (data.fileAttributes & (int) FileAttributes.Directory) != (int) FileAttributes.Directory; } else { if (!trueOnError) { return false; } else { // Return true if we cannot confirm that the file does NOT exist. int hr = Marshal.GetHRForLastWin32Error(); if (hr == HRESULT_WIN32_FILE_NOT_FOUND || hr == HRESULT_WIN32_PATH_NOT_FOUND) { return false; } else { return true; } } } } } } // 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
- EntityDescriptor.cs
- WpfKnownMemberInvoker.cs
- Int16Converter.cs
- DtdParser.cs
- ProtocolsConfigurationHandler.cs
- SqlCacheDependencyDatabaseCollection.cs
- Pens.cs
- Types.cs
- COM2ColorConverter.cs
- DbProviderSpecificTypePropertyAttribute.cs
- SafeNativeMethods.cs
- CollectionBase.cs
- StateMachineTimers.cs
- XmlTextEncoder.cs
- LoadWorkflowByKeyAsyncResult.cs
- MatrixTransform.cs
- XsltQilFactory.cs
- UrlMapping.cs
- ProvidersHelper.cs
- ServiceBusyException.cs
- Message.cs
- TransformGroup.cs
- PrintDialog.cs
- ArgumentException.cs
- ICspAsymmetricAlgorithm.cs
- PauseStoryboard.cs
- SqlServices.cs
- RSATokenProvider.cs
- Page.cs
- MonthChangedEventArgs.cs
- TextTreeInsertUndoUnit.cs
- DataBindingHandlerAttribute.cs
- ResourceAssociationSetEnd.cs
- EntityPropertyMappingAttribute.cs
- Transform.cs
- EncodingStreamWrapper.cs
- StubHelpers.cs
- WindowsPrincipal.cs
- SafeFileMappingHandle.cs
- NativeMethods.cs
- Group.cs
- MD5Cng.cs
- XpsSerializationManagerAsync.cs
- HandleTable.cs
- ColumnMapProcessor.cs
- TextStore.cs
- AssemblyAttributesGoHere.cs
- WindowInteractionStateTracker.cs
- SafeSecurityHandles.cs
- CodeDomConfigurationHandler.cs
- ISessionStateStore.cs
- SplineKeyFrames.cs
- ComponentChangedEvent.cs
- DetailsViewRow.cs
- ISAPIRuntime.cs
- RtfToXamlReader.cs
- MemberDescriptor.cs
- SmiSettersStream.cs
- CodeAssignStatement.cs
- AddingNewEventArgs.cs
- SequentialUshortCollection.cs
- LifetimeServices.cs
- Control.cs
- PerformanceCounterLib.cs
- RootBrowserWindowProxy.cs
- IndexingContentUnit.cs
- WebBaseEventKeyComparer.cs
- Currency.cs
- GridViewRowPresenter.cs
- SimpleHandlerBuildProvider.cs
- BitmapPalettes.cs
- UnauthorizedWebPart.cs
- SwitchLevelAttribute.cs
- shaperfactoryquerycacheentry.cs
- Brush.cs
- XPathPatternParser.cs
- SqlConnection.cs
- ISCIIEncoding.cs
- GlyphRunDrawing.cs
- XsltQilFactory.cs
- LogSwitch.cs
- CategoryAttribute.cs
- XmlText.cs
- RegisteredArrayDeclaration.cs
- ExtendedPropertyCollection.cs
- Zone.cs
- DataGridViewCellEventArgs.cs
- TableProviderWrapper.cs
- ipaddressinformationcollection.cs
- SymbolEqualComparer.cs
- login.cs
- DataGridViewRowHeightInfoNeededEventArgs.cs
- WebPartMovingEventArgs.cs
- Int32AnimationBase.cs
- ButtonChrome.cs
- BrowserCapabilitiesFactoryBase.cs
- _NetRes.cs
- VectorKeyFrameCollection.cs
- ExpandSegment.cs
- BrowserCapabilitiesFactory.cs