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
- DbParameterCollectionHelper.cs
- CharEntityEncoderFallback.cs
- XmlTextWriter.cs
- ValidatorCollection.cs
- DeviceContext.cs
- CommandBinding.cs
- ViewManager.cs
- TextElementAutomationPeer.cs
- PrimitiveType.cs
- TextCompositionManager.cs
- EntityProviderFactory.cs
- RepeaterItemEventArgs.cs
- Dispatcher.cs
- FakeModelPropertyImpl.cs
- HostExecutionContextManager.cs
- _CacheStreams.cs
- GlobalProxySelection.cs
- DispatcherObject.cs
- invalidudtexception.cs
- XamlReaderHelper.cs
- ContextQuery.cs
- UserControl.cs
- SchemaCollectionCompiler.cs
- CodeCompileUnit.cs
- ObjectViewFactory.cs
- DependencyObjectPropertyDescriptor.cs
- TrackingLocation.cs
- RequestBringIntoViewEventArgs.cs
- PointKeyFrameCollection.cs
- CurrentChangingEventManager.cs
- EntityStoreSchemaFilterEntry.cs
- OpenFileDialog.cs
- ThousandthOfEmRealPoints.cs
- WsatServiceAddress.cs
- DbParameterHelper.cs
- Span.cs
- PageThemeCodeDomTreeGenerator.cs
- StateMachineWorkflowDesigner.cs
- Menu.cs
- BorderSidesEditor.cs
- ImageFormatConverter.cs
- DecimalConstantAttribute.cs
- PowerStatus.cs
- DefaultTextStoreTextComposition.cs
- VerticalAlignConverter.cs
- DataServiceException.cs
- DataSourceExpression.cs
- ProcessModelSection.cs
- ProtectedProviderSettings.cs
- ReflectionHelper.cs
- GeneralTransform3D.cs
- OraclePermissionAttribute.cs
- ReferenceConverter.cs
- SmiEventStream.cs
- GeneralTransform3DTo2D.cs
- EntitySqlQueryBuilder.cs
- DataBoundLiteralControl.cs
- ByteStorage.cs
- TypeDelegator.cs
- WindowsEditBoxRange.cs
- SecurityContext.cs
- MappedMetaModel.cs
- PeerApplicationLaunchInfo.cs
- CqlParserHelpers.cs
- PixelShader.cs
- MessageBox.cs
- QueryCacheManager.cs
- Soap12ServerProtocol.cs
- BufferAllocator.cs
- RegexRunnerFactory.cs
- SessionEndingCancelEventArgs.cs
- CodePageEncoding.cs
- EmptyEnumerator.cs
- TextAnchor.cs
- FormViewUpdatedEventArgs.cs
- CodeGenerator.cs
- WindowsStartMenu.cs
- DataSourceComponent.cs
- DSASignatureDeformatter.cs
- ToolbarAUtomationPeer.cs
- ExpressionList.cs
- QilInvokeEarlyBound.cs
- UnsafeCollabNativeMethods.cs
- WeakReferenceList.cs
- CommandDevice.cs
- TextEditorLists.cs
- TrackingCondition.cs
- CheckBox.cs
- PropertyMappingExceptionEventArgs.cs
- ProtocolsConfiguration.cs
- HtmlTableCellCollection.cs
- AsyncCodeActivityContext.cs
- RegularExpressionValidator.cs
- WindowsListViewGroup.cs
- ProgressBarAutomationPeer.cs
- TCPListener.cs
- ActivityExecutor.cs
- XamlDesignerSerializationManager.cs
- DateTimeAutomationPeer.cs
- DesignerDataView.cs