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
- WebFormDesignerActionService.cs
- ConsoleTraceListener.cs
- HttpListenerTimeoutManager.cs
- GridViewPageEventArgs.cs
- COM2PropertyPageUITypeConverter.cs
- DataBindingCollection.cs
- ReadWriteSpinLock.cs
- EditingCoordinator.cs
- SendMessageContent.cs
- RuntimeConfigurationRecord.cs
- PeerCollaborationPermission.cs
- EdmComplexPropertyAttribute.cs
- TransferMode.cs
- UInt16.cs
- GACMembershipCondition.cs
- ToolStripProgressBar.cs
- UniformGrid.cs
- PerformanceCounterManager.cs
- ParameterModifier.cs
- Rect3DConverter.cs
- Int16AnimationBase.cs
- ValidationRuleCollection.cs
- XPathSelectionIterator.cs
- MergeFailedEvent.cs
- AsyncOperationContext.cs
- CapabilitiesSection.cs
- DependencyObjectPropertyDescriptor.cs
- ActivityStatusChangeEventArgs.cs
- WebPartConnectVerb.cs
- UnderstoodHeaders.cs
- EventLogRecord.cs
- DockProviderWrapper.cs
- DbModificationCommandTree.cs
- FormViewModeEventArgs.cs
- validationstate.cs
- PrintPreviewControl.cs
- HttpSessionStateWrapper.cs
- ImportOptions.cs
- XPathNavigatorReader.cs
- HyperlinkAutomationPeer.cs
- DashStyles.cs
- AssemblyResourceLoader.cs
- DesignTimeValidationFeature.cs
- SourceLocationProvider.cs
- WhiteSpaceTrimStringConverter.cs
- ConfigurationStrings.cs
- ReferenceEqualityComparer.cs
- ShaderRenderModeValidation.cs
- HtmlHistory.cs
- FileVersion.cs
- ResourceDisplayNameAttribute.cs
- RuntimeConfigLKG.cs
- DeadCharTextComposition.cs
- SkewTransform.cs
- PathSegmentCollection.cs
- ConcurrentStack.cs
- IsolatedStorageFileStream.cs
- TextInfo.cs
- MatrixKeyFrameCollection.cs
- EditableTreeList.cs
- DispatcherFrame.cs
- invalidudtexception.cs
- TransactedReceiveScope.cs
- BamlReader.cs
- arc.cs
- SmiEventSink_DeferedProcessing.cs
- RoamingStoreFileUtility.cs
- SByteConverter.cs
- SuppressMergeCheckAttribute.cs
- InstanceOwnerQueryResult.cs
- ProcessModelInfo.cs
- QueryContinueDragEventArgs.cs
- Rect.cs
- ExternalException.cs
- AssociationEndMember.cs
- SerializationStore.cs
- RuleSettingsCollection.cs
- HttpListenerTimeoutManager.cs
- BitArray.cs
- Camera.cs
- WebPartDescriptionCollection.cs
- _LazyAsyncResult.cs
- SudsCommon.cs
- RotationValidation.cs
- CommonDialog.cs
- WebPartActionVerb.cs
- DirectoryObjectSecurity.cs
- TextAnchor.cs
- TableAutomationPeer.cs
- ListViewGroupConverter.cs
- BindingManagerDataErrorEventArgs.cs
- HttpMethodAttribute.cs
- Animatable.cs
- ExistsInCollection.cs
- SafeSystemMetrics.cs
- TabControlEvent.cs
- PageStatePersister.cs
- PersonalizationDictionary.cs
- ExeConfigurationFileMap.cs
- PolyLineSegment.cs