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
- _SpnDictionary.cs
- SQLBinaryStorage.cs
- Schema.cs
- ColumnHeader.cs
- XamlClipboardData.cs
- BrowserCapabilitiesFactory35.cs
- DesignParameter.cs
- Identifier.cs
- SettingsBindableAttribute.cs
- HandleExceptionArgs.cs
- FormsAuthenticationConfiguration.cs
- Signature.cs
- AssemblyResourceLoader.cs
- TagMapInfo.cs
- DiscoveryMessageProperty.cs
- AsyncOperationManager.cs
- StrictModeSecurityHeaderElementInferenceEngine.cs
- EntityDesignerUtils.cs
- HttpListenerContext.cs
- HitTestResult.cs
- ToolStripDropDownButton.cs
- ToolStripDropDownItem.cs
- GatewayDefinition.cs
- ReceiveMessageAndVerifySecurityAsyncResultBase.cs
- WpfSharedBamlSchemaContext.cs
- ColorIndependentAnimationStorage.cs
- DataServiceRequestOfT.cs
- ILGenerator.cs
- ProxyWebPart.cs
- SpecialNameAttribute.cs
- ScriptingProfileServiceSection.cs
- NameValueCollection.cs
- UpdateCompiler.cs
- Attribute.cs
- WebEventCodes.cs
- MenuItem.cs
- TargetException.cs
- EmptyElement.cs
- ListItemParagraph.cs
- DataGridViewLayoutData.cs
- WinEventQueueItem.cs
- TextParentUndoUnit.cs
- ImageSourceConverter.cs
- Terminate.cs
- TextRangeSerialization.cs
- TypeConverterBase.cs
- CollectionEditor.cs
- PerformanceCounterPermission.cs
- NetworkCredential.cs
- CryptoStream.cs
- TextTreeUndo.cs
- XmlDigitalSignatureProcessor.cs
- BulletDecorator.cs
- Expressions.cs
- StorageEntityContainerMapping.cs
- CounterCreationData.cs
- DispatcherHooks.cs
- AutoResetEvent.cs
- XPathNode.cs
- WhiteSpaceTrimStringConverter.cs
- HttpModule.cs
- SecureUICommand.cs
- DesignBindingPropertyDescriptor.cs
- ThicknessAnimation.cs
- EntityDataSourceReferenceGroup.cs
- CallbackException.cs
- WindowsTokenRoleProvider.cs
- TraceXPathNavigator.cs
- EpmSourceTree.cs
- MenuBase.cs
- CompilerWrapper.cs
- PageThemeBuildProvider.cs
- FontConverter.cs
- AutomationPatternInfo.cs
- SpecialTypeDataContract.cs
- DocumentGrid.cs
- DateTimeSerializationSection.cs
- CalendarTable.cs
- EntityDataSourceReferenceGroup.cs
- ResourcesBuildProvider.cs
- TextStore.cs
- TextReader.cs
- JsonEncodingStreamWrapper.cs
- mactripleDES.cs
- XmlQualifiedName.cs
- ConfigXmlElement.cs
- TypeDescriptionProvider.cs
- ConstructorNeedsTagAttribute.cs
- UniqueConstraint.cs
- ProcessRequestAsyncResult.cs
- Window.cs
- EventSinkHelperWriter.cs
- PrintPreviewControl.cs
- RectAnimationClockResource.cs
- BCryptSafeHandles.cs
- WindowsSidIdentity.cs
- DataObjectAttribute.cs
- XmlSchemaProviderAttribute.cs
- MenuCommandService.cs
- JsonWriter.cs