[System] Process.WaitForExit now triggers event Exited.
[mono.git] / mcs / class / corlib / System.IO / FileNotFoundException.cs
1 //
2 // System.IO.FileNotFoundException.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //   Duncan Mak (duncan@ximian.com)
7 //
8 // (C) 2001 Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Globalization;
32 using System.Runtime.Serialization;
33 using System.Security.Permissions;
34 using System.Text;
35 using System.Runtime.InteropServices;
36
37 namespace System.IO {
38
39         [Serializable]
40         [ComVisible (true)]
41         public class FileNotFoundException : IOException {
42
43                 const int Result = unchecked ((int)0x80131621);
44
45                 private string fileName;
46                 private string fusionLog;
47
48                 // Constructors
49                 public FileNotFoundException ()
50                         : base (Locale.GetText ("Unable to find the specified file."))
51                 {
52                         HResult = Result;
53                 }
54
55                 public FileNotFoundException (string message)
56                         : base (message)
57                 {
58                         HResult = Result;
59                 }
60
61                 public FileNotFoundException (string message, Exception innerException)
62                         : base (message, innerException)
63                 {
64                         HResult = Result;
65                 }
66
67                 public FileNotFoundException (string message, string fileName)
68                         : base (message)
69                 {
70                         HResult = Result;
71                         this.fileName = fileName;
72                 }
73
74                 public FileNotFoundException (string message, string fileName, Exception innerException)
75                         : base (message, innerException)
76                 {
77                         HResult = Result;
78                         this.fileName = fileName;
79                 }
80
81                 protected FileNotFoundException (SerializationInfo info, StreamingContext context)
82                         : base (info, context)
83                 {
84                         fileName = info.GetString ("FileNotFound_FileName");
85                         fusionLog = info.GetString ("FileNotFound_FusionLog");
86                 }
87
88                 public string FileName
89                 {
90                         get { return fileName; }
91                 }
92
93                 public string FusionLog {
94                         // note: MS runtime throws a SecurityException when the Exception is created
95                         // but a FileLoadException once the exception as been thrown. Mono always
96                         // throw a SecurityException in both case (anyway fusionLog is currently empty)
97                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
98                         get { return fusionLog; }
99                 }
100
101                 public override string Message {
102                         get {
103                                 if (base.message == null) {
104                                         if (fileName != null) {
105                                                 string message = string.Format (
106                                                         "Could not load file or assembly '{0}' or one of"
107                                                         + " its dependencies. The system cannot find the"
108                                                         + " file specified.", fileName);
109                                                 return message;
110                                         }
111                                 }
112                                 return base.message;
113                         }
114                 }
115
116                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
117                 {
118                         base.GetObjectData (info, context);
119                         info.AddValue ("FileNotFound_FileName", fileName);
120                         info.AddValue ("FileNotFound_FusionLog", fusionLog);
121                 }
122
123                 public override string ToString ()
124                 {
125                         StringBuilder sb = new StringBuilder (GetType ().FullName);
126                         sb.AppendFormat (": {0}", Message);
127
128                         if (fileName != null && fileName.Length > 0) {
129                                 sb.Append (Environment.NewLine);
130                                 sb.AppendFormat ("File name: '{0}'", fileName);
131                         }
132
133                         if (this.InnerException != null)
134                                 sb.AppendFormat (" ---> {0}", InnerException);
135
136                         if (this.StackTrace != null) {
137                                 sb.Append (Environment.NewLine);
138                                 sb.Append (StackTrace);
139                         }
140
141                         return sb.ToString ();
142                 }
143         }
144 }