System.IO.FileNotFoundException Class

public class FileNotFoundException : IOException

Base Types

Object
  Exception
    SystemException
      IOException
        FileNotFoundException

Assembly

mscorlib

Library

BCL

Summary

Represents the error that occurs when a file path argument specifies a file that does not exist.

Example

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".

See Also

System.IO Namespace

Members

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


FileNotFoundException() Constructor

public FileNotFoundException();

Summary

Constructs and initializes a new instance of the FileNotFoundException class.

Description

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 .

See Also

System.IO.FileNotFoundException Class, System.IO Namespace

FileNotFoundException(System.String) Constructor

public FileNotFoundException(string message);

Summary

Constructs and initializes a new instance of the FileNotFoundException class.

Parameters

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.

Description

This constructor initializes the System.IO.FileNotFoundException.Message property of the new instance using message. If message is null , 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 .

See Also

System.IO.FileNotFoundException Class, System.IO Namespace

FileNotFoundException(System.String, System.Exception) Constructor

public FileNotFoundException(string message, Exception innerException);

Summary

Constructs and initializes a new instance of the FileNotFoundException class.

Parameters

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.

Description

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 is null , 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.]

See Also

System.IO.FileNotFoundException Class, System.IO Namespace

FileNotFoundException(System.String, System.String) Constructor

public FileNotFoundException(string message, string fileName);

Summary

Constructs and initializes a new instance of the FileNotFoundException class.

Parameters

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.

Description

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 is null , 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 .

See Also

System.IO.FileNotFoundException Class, System.IO Namespace

FileNotFoundException(System.String, System.String, System.Exception) Constructor

public FileNotFoundException(string message, string fileName, Exception innerException);

Summary

Constructs and initializes a new instance of the FileNotFoundException class.

Parameters

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.

Description

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 is null , 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.]

See Also

System.IO.FileNotFoundException Class, System.IO Namespace

FileNotFoundException.ToString Method

public override string ToString();

Summary

Returns a String representation of the current instance.

Return Value

A String representation of the current Exception.

Description

The string representation returned by this method includes the fully qualified name of the current Exception, the message, the result of calling ToString 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.]

Example

The following example causes a FileNotFoundException exception and displays the result of calling ToString 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()
 

See Also

System.IO.FileNotFoundException Class, System.IO Namespace

FileNotFoundException.FileName Property

public string FileName { get; }

Summary

Gets the name of the file that cannot be found.

Property Value

A String containing the name of the file, or null if no file name was passed to the constructor for the current instance.

Description

This property is read-only.

See Also

System.IO.FileNotFoundException Class, System.IO Namespace

FileNotFoundException.Message Property

public override string Message { get; }

Summary

Gets the error message that explains the reason for the exception.

Property Value

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.

Description

This property is read-only.

See Also

System.IO.FileNotFoundException Class, System.IO Namespace