Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / Configuration / System / Configuration / FileUtil.cs / 1 / 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
- SafeMemoryMappedFileHandle.cs
- PlainXmlDeserializer.cs
- OleDbErrorCollection.cs
- InfoCardSymmetricCrypto.cs
- WebBrowserProgressChangedEventHandler.cs
- TextLineResult.cs
- PathGradientBrush.cs
- Rectangle.cs
- AttachedPropertyInfo.cs
- AppDomainFactory.cs
- ListQueryResults.cs
- Util.cs
- Canvas.cs
- XmlSchemaImporter.cs
- Pair.cs
- Point4D.cs
- XPathNavigatorKeyComparer.cs
- DataBindingCollection.cs
- ContractSearchPattern.cs
- DescriptionAttribute.cs
- SignatureResourcePool.cs
- Image.cs
- COMException.cs
- TakeOrSkipQueryOperator.cs
- DBConnectionString.cs
- DetailsViewInsertedEventArgs.cs
- EngineSiteSapi.cs
- Propagator.JoinPropagator.cs
- ResetableIterator.cs
- X509Certificate2.cs
- ListViewItemCollectionEditor.cs
- HashStream.cs
- TreeNodeBindingCollection.cs
- WindowInteropHelper.cs
- PasswordPropertyTextAttribute.cs
- PageBuildProvider.cs
- HttpRequest.cs
- Annotation.cs
- ButtonChrome.cs
- DataGridViewCellContextMenuStripNeededEventArgs.cs
- Registry.cs
- XsdBuilder.cs
- Base64Encoding.cs
- clipboard.cs
- SystemPens.cs
- KerberosSecurityTokenAuthenticator.cs
- DelegateSerializationHolder.cs
- Script.cs
- ListControlBoundActionList.cs
- IgnoreSection.cs
- InstanceData.cs
- CapabilitiesPattern.cs
- ProtocolsConfigurationHandler.cs
- DragAssistanceManager.cs
- SiteMapNodeCollection.cs
- XXXOnTypeBuilderInstantiation.cs
- XmlDataSource.cs
- BrowserCapabilitiesFactory35.cs
- OleDbErrorCollection.cs
- AssemblyNameProxy.cs
- RuleInfoComparer.cs
- _NegoStream.cs
- Propagator.JoinPropagator.JoinPredicateVisitor.cs
- ConnectionManagementSection.cs
- BuildManager.cs
- FileRegion.cs
- PermissionSetEnumerator.cs
- HelpEvent.cs
- DerivedKeySecurityToken.cs
- HtmlEmptyTagControlBuilder.cs
- WindowsAuthenticationEventArgs.cs
- ObfuscateAssemblyAttribute.cs
- SelectedGridItemChangedEvent.cs
- DetailsViewPageEventArgs.cs
- ClientRolePrincipal.cs
- TypedTableHandler.cs
- ScriptResourceInfo.cs
- ListViewItemEventArgs.cs
- HttpRequest.cs
- QilName.cs
- NetMsmqSecurity.cs
- ToolStripSeparatorRenderEventArgs.cs
- HtmlUtf8RawTextWriter.cs
- CharKeyFrameCollection.cs
- DoubleLinkList.cs
- BadImageFormatException.cs
- ContentType.cs
- RequestQueue.cs
- ConfigurationPropertyAttribute.cs
- FrameworkElementAutomationPeer.cs
- StringArrayConverter.cs
- InputReportEventArgs.cs
- DesignerLoader.cs
- DetailsViewRow.cs
- SelectingProviderEventArgs.cs
- XmlCharacterData.cs
- MultiByteCodec.cs
- SelectionRange.cs
- MdiWindowListStrip.cs
- BitmapFrameDecode.cs