DirectoryNotFoundException Class in VB.NET

In this article I will explain you about the DirectoryNotFoundException Class in VB.NET.
  • 2989
 

As my previous article shows you how we use EventLog class to view the information about Log Files, System info, Active Directory etc. So, if the Directory was not found than how we work with this article show you about that procedure in VB.NET. We use a System.Io Namespace class to work with which names DirectoryNotFoundException Class, "The exception that is thrown when part of a file or directory cannot be found" by this class.

Syntax

    'Declaration
    <SerializableAttribute> _
    <ComVisibleAttribute(
True)> _
        Public Class DirectoryNotFoundException _
            Inherits IOException
 

DirectoryNotFoundException uses the HRESULT COR_E_DIRECTORYNOTFOUND which has the value 0x80070003. Note that DirectoryNotFoundException is also thrown when COM interop programs see the HRESULT STG_E_PATHNOTFOUND, which has the value 0x80030003.

If your code does not have PathDiscovery permission, the error message for this exception may only contain file or directory names instead of fully qualified paths.

The DirectoryNotFoundException type exposes the following members:

Constructors

Methods

Properties

Events

DirectoryNotFoundException Equals(Object) Data SerializeObjectState
DirectoryNotFoundException(String) Finalize HelpLink  
DirectoryNotFoundException(SerializationInfo, StreamingContext) GetBaseException HResult  
DirectoryNotFoundException(String, Exception) GetHashCode InnerException  
  GetType Message  
  MemberwiseClone Source  
  ToString StackTrace  
    TargetSite  

The following example shows how to force and recover from a DirectoryNotFoundException.

Example

    Imports
System.IO
    Module Module1
        Sub Main()
            Try
                ' Specify a directory name that does not exist for this demo.
                Dim dir As String = "c\fhdsghfbdjk"
                ' If this directory does not exist, a DirectoryNotFoundException is thrown
                ' when attempting to set the current directory.
                Directory.SetCurrentDirectory(dir)
                Catch ex As System.IO.DirectoryNotFoundException
                    ' Let the user know that the directory did not exist.
                    Console.WriteLine("Directory not found: " + ex.Message)
            End Try
                Console.ReadLine()
        End Sub
    End
Module

Output

dnf1.gif
 

Conclusion

Hope this article would have helped you in understanding DirectoryNotFoundException Class in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.