public class FileNotFoundException : IOException
Object
Exception
SystemException
IOException
FileNotFoundException
mscorlib
BCL
Represents the error that occurs when a file path argument specifies a file that does not exist.
The following example demonstrates an error that causes the FileNotFoundException exception to be thrown.
using System; using System.IO; class FileNotFoundExample { public static void Main () { string badPath = "/Eccma/examples/FileTest.cs"; string goodPath = "/Ecma/examples2/FileTest.cs"; try { File.Copy(badPath,goodPath); } catch (FileNotFoundException e) { Console.WriteLine("Caught: {0}",e.Message); } } }The output is
Caught: Could not find file "/Eccma/examples/FileTest.cs".
System.IO Namespace
FileNotFoundException Constructors
FileNotFoundException() Constructor
FileNotFoundException(System.String) Constructor
FileNotFoundException(System.String, System.Exception) Constructor
FileNotFoundException(System.String, System.String) Constructor
FileNotFoundException(System.String, System.String, System.Exception) Constructor
FileNotFoundException Methods
FileNotFoundException.ToString Method
FileNotFoundException Properties
FileNotFoundException.FileName Property
FileNotFoundException.Message Property
public FileNotFoundException();
Constructs and initializes a new instance of the FileNotFoundException class.
This constructor initializes the System.IO.FileNotFoundException.Message property of the new instance to a system-supplied message that describes the error, such as "Could not find the specified file." This message takes into account the current system culture.The System.IO.FileNotFoundException.InnerException and System.IO.FileNotFoundException.FileName properties of the new instance are initialized to
null
.
System.IO.FileNotFoundException Class, System.IO Namespace
public FileNotFoundException(string message);
Constructs and initializes a new instance of the FileNotFoundException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
This constructor initializes the System.IO.FileNotFoundException.Message property of the new instance using message. If message isnull
, the System.IO.FileNotFoundException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.The System.IO.FileNotFoundException.InnerException and System.IO.FileNotFoundException.FileName properties of the new instance are initialized to
null
.
System.IO.FileNotFoundException Class, System.IO Namespace
public FileNotFoundException(string message, Exception innerException);
Constructs and initializes a new instance of the FileNotFoundException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
- innerException
- An instance of Exception that is the cause of the current exception. If innerException is non-null, then the current Exception was raised in a catch block handling innerException.
This constructor initializes the System.IO.FileNotFoundException.Message property of the new instance using message and the System.IO.FileNotFoundException.InnerException property using innerException. If message isnull
, the System.IO.FileNotFoundException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.The System.IO.FileNotFoundException.FileName property of the new instance is initialized to
null
.[Note: For more information on inner exceptions, see System.Exception.InnerException.]
System.IO.FileNotFoundException Class, System.IO Namespace
public FileNotFoundException(string message, string fileName);
Constructs and initializes a new instance of the FileNotFoundException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
- fileName
- A String containing the name of the file that was not found.
The constructor initializes the System.IO.FileNotFoundException.Message property of the new instance using message and the System.IO.FileNotFoundException.FileName property using fileName. If message isnull
, the System.IO.FileNotFoundException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.The System.IO.FileNotFoundException.InnerException property of the new instance is initialized to
null
.
System.IO.FileNotFoundException Class, System.IO Namespace
public FileNotFoundException(string message, string fileName, Exception innerException);
Constructs and initializes a new instance of the FileNotFoundException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
- fileName
- The name of the file that was not found.
- innerException
- An instance of Exception that is the cause of the current exception. If innerException is non-null, then the current Exception was raised in a catch block handling innerException.
This constructor initializes the System.IO.FileNotFoundException.Message property of the new instance using message, the System.IO.FileNotFoundException.FileName property using fileName, and the System.IO.FileNotFoundException.InnerException property using innerException. If message isnull
, the System.IO.FileNotFoundException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.[Note: For more information on inner exceptions, see System.Exception.InnerException.]
System.IO.FileNotFoundException Class, System.IO Namespace
public override string ToString();
Returns a String representation of the current instance.
AString
representation of the current Exception.
The string representation returned by this method includes the fully qualified name of the current Exception, the message, the result of callingToString
on the inner Exception, the file name, and the result of calling System.Environment.StackTrace. Null values are omitted from the returned string.[Note: This method overrides System.Object.ToString.]
The following example causes a FileNotFoundException exception and displays the result of callingToString
on that Exception.
using System; using System.IO; class FileNotFoundExample { public static void Main () { string badPath = "/Eccma/examples/FileTest.cs"; string goodPath = "/Ecma/examples2/FileTest.cs"; try { File.Copy(badPath,goodPath); } catch (FileNotFoundException e) { Console.WriteLine("Caught: {0}",e.ToString()); } } }The output is
Caught: System.IO.FileNotFoundException: Could not find file "/Eccma/examples/FileTest.cs". File name: "/Eccma/examples/FileTest.cs" at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite) at FileNotFoundExample.Main()
System.IO.FileNotFoundException Class, System.IO Namespace
public string FileName { get; }
Gets the name of the file that cannot be found.
A String containing the name of the file, ornull
if no file name was passed to the constructor for the current instance.
This property is read-only.
System.IO.FileNotFoundException Class, System.IO Namespace
public override string Message { get; }
Gets the error message that explains the reason for the exception.
A String containing the error message. This string includes the System.IO.FileNotFoundException.FileName if a value for that property was supplied to the constructor for the current instance.
This property is read-only.
System.IO.FileNotFoundException Class, System.IO Namespace