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
- WindowCollection.cs
- ToolBarButtonDesigner.cs
- ToolStripTemplateNode.cs
- FieldValue.cs
- ProcessModuleDesigner.cs
- SocketPermission.cs
- linebase.cs
- DBConnection.cs
- SessionEndedEventArgs.cs
- PresentationTraceSources.cs
- ContentIterators.cs
- WebPartDeleteVerb.cs
- TextServicesHost.cs
- AppDomainResourcePerfCounters.cs
- X509InitiatorCertificateServiceElement.cs
- XmlReflectionImporter.cs
- EventMappingSettings.cs
- MenuAdapter.cs
- AuthenticatingEventArgs.cs
- XmlWhitespace.cs
- ForEachAction.cs
- ItemType.cs
- RectangleF.cs
- XmlSchemaException.cs
- Span.cs
- FixedSOMGroup.cs
- LinkedList.cs
- Rect.cs
- PopupRootAutomationPeer.cs
- ContextInformation.cs
- EdmConstants.cs
- GenericUriParser.cs
- AppDomainFactory.cs
- StrokeRenderer.cs
- TextDocumentView.cs
- mediaeventargs.cs
- EmbeddedMailObjectsCollection.cs
- SystemResources.cs
- StreamSecurityUpgradeInitiatorAsyncResult.cs
- _SpnDictionary.cs
- Trace.cs
- SerializationInfo.cs
- DataGridViewColumn.cs
- ProgressPage.cs
- CanonicalizationDriver.cs
- Rect3DValueSerializer.cs
- TouchFrameEventArgs.cs
- ValidationEventArgs.cs
- IPEndPoint.cs
- DllNotFoundException.cs
- NetworkInterface.cs
- MemoryRecordBuffer.cs
- CalendarDateChangedEventArgs.cs
- ExpandSegment.cs
- Event.cs
- OleDbEnumerator.cs
- Cursor.cs
- RowSpanVector.cs
- BrushMappingModeValidation.cs
- VariableQuery.cs
- ValidationService.cs
- Debugger.cs
- DesignerView.xaml.cs
- FontInfo.cs
- ColorTranslator.cs
- SchemaDeclBase.cs
- RC2.cs
- FontStretchConverter.cs
- TreeNodeSelectionProcessor.cs
- FigureHelper.cs
- AccessDataSourceView.cs
- SpinLock.cs
- NamespaceList.cs
- FrameworkElement.cs
- AssemblyInfo.cs
- DataGridDetailsPresenterAutomationPeer.cs
- CheckBoxStandardAdapter.cs
- SqlMethodAttribute.cs
- SEHException.cs
- IISUnsafeMethods.cs
- FileEnumerator.cs
- SessionSwitchEventArgs.cs
- Convert.cs
- HttpCacheParams.cs
- BindingSourceDesigner.cs
- ConnectivityStatus.cs
- CompilationPass2TaskInternal.cs
- EntityDataSourceColumn.cs
- SessionStateUtil.cs
- TraceInternal.cs
- Command.cs
- DataGridViewCellContextMenuStripNeededEventArgs.cs
- AdRotator.cs
- TableSectionStyle.cs
- sapiproxy.cs
- XmlQueryRuntime.cs
- AsyncInvokeOperation.cs
- IisTraceListener.cs
- _NetRes.cs
- SendKeys.cs