985d9e1a1c349b14ee01aceb50e56a3addedd84f
[mono.git] / mcs / class / referencesource / mscorlib / system / io / filenotfoundexception.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class:  FileNotFoundException
9 ** 
10 ** <OWNER>Microsoft</OWNER>
11 ** <OWNER>Microsoft</OWNER>
12 **
13 **
14 ** Purpose: Exception for accessing a file that doesn't exist.
15 **
16 **
17 ===========================================================*/
18
19 using System;
20 using System.Runtime.Serialization;
21 using System.Security.Permissions;
22 using SecurityException = System.Security.SecurityException;
23 using System.Globalization;
24
25 namespace System.IO {
26     // Thrown when trying to access a file that doesn't exist on disk.
27     [Serializable]
28 [System.Runtime.InteropServices.ComVisible(true)]
29     public class FileNotFoundException : IOException {
30
31         private String _fileName;  // The name of the file that isn't found.
32         private String _fusionLog;  // fusion log (when applicable)
33         
34         public FileNotFoundException() 
35             : base(Environment.GetResourceString("IO.FileNotFound")) {
36             SetErrorCode(__HResults.COR_E_FILENOTFOUND);
37         }
38     
39         public FileNotFoundException(String message) 
40             : base(message) {
41             SetErrorCode(__HResults.COR_E_FILENOTFOUND);
42         }
43     
44         public FileNotFoundException(String message, Exception innerException) 
45             : base(message, innerException) {
46             SetErrorCode(__HResults.COR_E_FILENOTFOUND);
47         }
48
49         public FileNotFoundException(String message, String fileName) : base(message)
50         {
51             SetErrorCode(__HResults.COR_E_FILENOTFOUND);
52             _fileName = fileName;
53         }
54
55         public FileNotFoundException(String message, String fileName, Exception innerException) 
56             : base(message, innerException) {
57             SetErrorCode(__HResults.COR_E_FILENOTFOUND);
58             _fileName = fileName;
59         }
60
61         public override String Message
62         {
63             get {
64                 SetMessageField();
65                 return _message;
66             }
67         }
68
69         private void SetMessageField()
70         {
71             if (_message == null) {
72                 if ((_fileName == null) &&
73                     (HResult == System.__HResults.COR_E_EXCEPTION))
74                     _message = Environment.GetResourceString("IO.FileNotFound");
75
76                 else if( _fileName != null)
77                     _message = FileLoadException.FormatFileLoadExceptionMessage(_fileName, HResult);
78             }
79         }
80
81         public String FileName {
82             get { return _fileName; }
83         }
84
85         public override String ToString()
86         {
87             String s = GetType().FullName + ": " + Message;
88
89             if (_fileName != null && _fileName.Length != 0)
90                 s += Environment.NewLine + Environment.GetResourceString("IO.FileName_Name", _fileName);
91             
92             if (InnerException != null)
93                 s = s + " ---> " + InnerException.ToString();
94
95             if (StackTrace != null)
96                 s += Environment.NewLine + StackTrace;
97
98 #if FEATURE_FUSION            
99             try
100             {
101                 if(FusionLog!=null)
102                 {
103                     if (s==null)
104                         s=" ";
105                     s+=Environment.NewLine;
106                     s+=Environment.NewLine;
107                     s+=FusionLog;
108                 }
109             }
110             catch(SecurityException)
111             {
112             
113             }
114 #endif            
115             return s;
116             
117         }
118
119         protected FileNotFoundException(SerializationInfo info, StreamingContext context) : base (info, context) {
120             // Base class constructor will check info != null.
121
122             _fileName = info.GetString("FileNotFound_FileName");
123             try
124             {
125                 _fusionLog = info.GetString("FileNotFound_FusionLog");
126             }
127             catch 
128             {
129                 _fusionLog = null;
130             }
131             
132         }
133
134         private FileNotFoundException(String fileName, String fusionLog,int hResult)
135             : base(null)
136         {
137             SetErrorCode(hResult);
138             _fileName = fileName;
139             _fusionLog=fusionLog;
140             SetMessageField();
141         }
142
143 #if FEATURE_FUSION
144         public String FusionLog {
145             [System.Security.SecuritySafeCritical]  // auto-generated
146             [SecurityPermissionAttribute( SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlEvidence | SecurityPermissionFlag.ControlPolicy)]
147             get { return _fusionLog; }
148         }
149 #endif
150
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);
156
157             // Serialize data for this class
158             info.AddValue("FileNotFound_FileName", _fileName, typeof(String));
159
160             try
161             {
162                 info.AddValue("FileNotFound_FusionLog", FusionLog, typeof(String));
163             }
164             catch (SecurityException)
165             {
166             }
167         }
168 #endif
169     }
170 }
171