System.IO.FileNotFoundException Class

The exception that is thrown when an attempt to access a file that does not exist on disk fails.

See Also: FileNotFoundException Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public class FileNotFoundException : IOException

Remarks

FileNotFoundException uses the HRESULT COR_E_FILENOTFOUND which has the value 0x80070002.

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

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Example

The following example demonstrates an error that causes the System.IO.FileNotFoundException exception to be thrown.

C# Example

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

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0