3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
8 ** Class: FileNotFoundException
10 ** <OWNER>Microsoft</OWNER>
11 ** <OWNER>Microsoft</OWNER>
14 ** Purpose: Exception for accessing a file that doesn't exist.
17 ===========================================================*/
20 using System.Runtime.Serialization;
21 using System.Security.Permissions;
22 using SecurityException = System.Security.SecurityException;
23 using System.Globalization;
26 // Thrown when trying to access a file that doesn't exist on disk.
28 [System.Runtime.InteropServices.ComVisible(true)]
29 public class FileNotFoundException : IOException {
31 private String _fileName; // The name of the file that isn't found.
32 private String _fusionLog; // fusion log (when applicable)
34 public FileNotFoundException()
35 : base(Environment.GetResourceString("IO.FileNotFound")) {
36 SetErrorCode(__HResults.COR_E_FILENOTFOUND);
39 public FileNotFoundException(String message)
41 SetErrorCode(__HResults.COR_E_FILENOTFOUND);
44 public FileNotFoundException(String message, Exception innerException)
45 : base(message, innerException) {
46 SetErrorCode(__HResults.COR_E_FILENOTFOUND);
49 public FileNotFoundException(String message, String fileName) : base(message)
51 SetErrorCode(__HResults.COR_E_FILENOTFOUND);
55 public FileNotFoundException(String message, String fileName, Exception innerException)
56 : base(message, innerException) {
57 SetErrorCode(__HResults.COR_E_FILENOTFOUND);
61 public override String Message
69 private void SetMessageField()
71 if (_message == null) {
72 if ((_fileName == null) &&
73 (HResult == System.__HResults.COR_E_EXCEPTION))
74 _message = Environment.GetResourceString("IO.FileNotFound");
76 else if( _fileName != null)
77 _message = FileLoadException.FormatFileLoadExceptionMessage(_fileName, HResult);
81 public String FileName {
82 get { return _fileName; }
85 public override String ToString()
87 String s = GetType().FullName + ": " + Message;
89 if (_fileName != null && _fileName.Length != 0)
90 s += Environment.NewLine + Environment.GetResourceString("IO.FileName_Name", _fileName);
92 if (InnerException != null)
93 s = s + " ---> " + InnerException.ToString();
95 if (StackTrace != null)
96 s += Environment.NewLine + StackTrace;
105 s+=Environment.NewLine;
106 s+=Environment.NewLine;
110 catch(SecurityException)
119 protected FileNotFoundException(SerializationInfo info, StreamingContext context) : base (info, context) {
120 // Base class constructor will check info != null.
122 _fileName = info.GetString("FileNotFound_FileName");
125 _fusionLog = info.GetString("FileNotFound_FusionLog");
134 private FileNotFoundException(String fileName, String fusionLog,int hResult)
137 SetErrorCode(hResult);
138 _fileName = fileName;
139 _fusionLog=fusionLog;
144 public String FusionLog {
145 [System.Security.SecuritySafeCritical] // auto-generated
146 [SecurityPermissionAttribute( SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlEvidence | SecurityPermissionFlag.ControlPolicy)]
147 get { return _fusionLog; }
151 #if FEATURE_SERIALIZATION
152 [System.Security.SecurityCritical] // auto-generated_required
153 public override void GetObjectData(SerializationInfo info, StreamingContext context) {
154 // Serialize data for our base classes. base will verify info != null.
155 base.GetObjectData(info, context);
157 // Serialize data for this class
158 info.AddValue("FileNotFound_FileName", _fileName, typeof(String));
162 info.AddValue("FileNotFound_FusionLog", FusionLog, typeof(String));
164 catch (SecurityException)