System.IO.FileNotFoundException.ToString Method

Returns the fully qualified name of this exception and possibly the error message, the name of the inner exception, and the stack trace.

Syntax

public override string ToString ()

Returns

The fully qualified name of this exception and possibly the error message, the name of the inner exception, and the stack trace.

Remarks

This method overrides object.ToString.

The string representation returned by this method includes the name of the exception, the value of the FileNotFoundException.Message property, the result of calling ToString on the inner exception, the value of the FileNotFoundException.FileName property, and the result of calling Environment.StackTrace. If any of these members is a null reference, its value is not included in the returned string.

Example

The following example causes a System.IO.FileNotFoundException exception and displays the result of calling ToString on that Exception.

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.ToString());
    }
  }
}

The output is

Example

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()
 

Requirements

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