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
- InvalidDocumentContentsException.cs
- ScriptResourceHandler.cs
- CheckBox.cs
- DynamicActivityTypeDescriptor.cs
- EdmType.cs
- SynchronizedInputPattern.cs
- ComplexBindingPropertiesAttribute.cs
- ConditionCollection.cs
- SQLCharsStorage.cs
- UInt16Storage.cs
- RegexBoyerMoore.cs
- PathData.cs
- ApplicationSettingsBase.cs
- LogPolicy.cs
- Positioning.cs
- DateTimeConverter.cs
- SmtpSpecifiedPickupDirectoryElement.cs
- URLIdentityPermission.cs
- SafeHandles.cs
- CompositeTypefaceMetrics.cs
- CodeCastExpression.cs
- QilName.cs
- PageVisual.cs
- TraceContextEventArgs.cs
- ReferenceEqualityComparer.cs
- MetricEntry.cs
- AppSettingsExpressionBuilder.cs
- RowUpdatingEventArgs.cs
- Util.cs
- ListDictionary.cs
- UserPreferenceChangedEventArgs.cs
- SiteMapDataSource.cs
- TypeGeneratedEventArgs.cs
- autovalidator.cs
- MonthChangedEventArgs.cs
- CustomActivityDesigner.cs
- SqlDataSourceCustomCommandPanel.cs
- OdbcConnectionPoolProviderInfo.cs
- ServerValidateEventArgs.cs
- ConstructorNeedsTagAttribute.cs
- PlatformNotSupportedException.cs
- QueryCacheEntry.cs
- ParentQuery.cs
- PropertyPathConverter.cs
- Comparer.cs
- PlanCompilerUtil.cs
- UnsafeNativeMethods.cs
- PriorityBindingExpression.cs
- InteropBitmapSource.cs
- TypeName.cs
- HttpListenerPrefixCollection.cs
- TableCell.cs
- RangeContentEnumerator.cs
- unsafenativemethodsother.cs
- ContourSegment.cs
- XmlIlVisitor.cs
- PaginationProgressEventArgs.cs
- EventLogEntryCollection.cs
- TextTreeRootNode.cs
- WebAdminConfigurationHelper.cs
- CustomAttribute.cs
- FunctionCommandText.cs
- MsmqIntegrationAppDomainProtocolHandler.cs
- RemoteTokenFactory.cs
- PeerOutputChannel.cs
- AggregateNode.cs
- EntityDataSourceWrapper.cs
- SiteMapNode.cs
- _FixedSizeReader.cs
- TiffBitmapEncoder.cs
- WebPartHeaderCloseVerb.cs
- HttpCapabilitiesBase.cs
- SecurityAttributeGenerationHelper.cs
- FloaterBaseParagraph.cs
- ObjectListDataBindEventArgs.cs
- ToolStripHighContrastRenderer.cs
- DataGridViewDataConnection.cs
- TrustManagerPromptUI.cs
- Type.cs
- OlePropertyStructs.cs
- BindingSource.cs
- QilInvokeEarlyBound.cs
- SchemaEntity.cs
- System.Data.OracleClient_BID.cs
- HandlerWithFactory.cs
- ConfigurationElement.cs
- XhtmlBasicCommandAdapter.cs
- UserMapPath.cs
- SimpleApplicationHost.cs
- CustomErrorCollection.cs
- Route.cs
- SqlPersonalizationProvider.cs
- SecurityRuntime.cs
- Processor.cs
- GeometryConverter.cs
- XPathDocument.cs
- Int32.cs
- IdentityReference.cs
- TextDecoration.cs
- DataGridViewRowStateChangedEventArgs.cs