2008-05-14 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[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
36 #if NET_2_0
37 using System.Runtime.InteropServices;
38 #endif
39
40 namespace System.IO {
41
42         [Serializable]
43 #if NET_2_0
44         [ComVisible (true)]
45 #endif
46         public class FileNotFoundException : IOException {
47
48                 const int Result = unchecked ((int)0x80131621);
49
50                 private string fileName;
51                 private string fusionLog;
52
53                 // Constructors
54                 public FileNotFoundException ()
55                         : base (Locale.GetText ("Unable to find the specified file."))
56                 {
57                         HResult = Result;
58                 }
59
60                 public FileNotFoundException (string message)
61                         : base (message)
62                 {
63                         HResult = Result;
64                 }
65
66                 public FileNotFoundException (string message, Exception inner)
67                         : base (message, inner)
68                 {
69                         HResult = Result;
70                 }
71
72                 public FileNotFoundException (string message, string fileName)
73                         : base (message)
74                 {
75                         HResult = Result;
76                         this.fileName = fileName;
77                 }
78
79                 public FileNotFoundException (string message, string fileName, Exception innerException)
80                         : base (message, innerException)
81                 {
82                         HResult = Result;
83                         this.fileName = fileName;
84                 }
85
86                 protected FileNotFoundException (SerializationInfo info, StreamingContext context)
87                         : base (info, context)
88                 {
89                         fileName = info.GetString ("FileNotFound_FileName");
90                         fusionLog = info.GetString ("FileNotFound_FusionLog");
91                 }
92
93                 public string FileName
94                 {
95                         get { return fileName; }
96                 }
97
98                 public string FusionLog {
99                         // note: MS runtime throws a SecurityException when the Exception is created
100                         // but a FileLoadException once the exception as been thrown. Mono always
101                         // throw a SecurityException in both case (anyway fusionLog is currently empty)
102                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
103                         get { return fusionLog; }
104                 }
105
106                 public override string Message {
107                         get {
108                                 if (base.message == null) {
109 #if NET_2_0
110                                         if (fileName != null) {
111                                                 string message = string.Format (CultureInfo.CurrentCulture,
112                                                         "Could not load file or assembly '{0}' or one of"
113                                                         + " its dependencies. The system cannot find the"
114                                                         + " file specified.", fileName);
115                                                 return message;
116                                         }
117 #else
118                                         string file = fileName == null ? "(null)" : fileName;
119                                         string message = string.Format (CultureInfo.CurrentCulture,
120                                                 "File or assembly name {0}, or one of its dependencies,"
121                                                 + " was not found.", file);
122                                         return message;
123 #endif
124                                 }
125                                 return base.message;
126                         }
127                 }
128
129                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
130                 {
131                         base.GetObjectData (info, context);
132                         info.AddValue ("FileNotFound_FileName", fileName);
133                         info.AddValue ("FileNotFound_FusionLog", fusionLog);
134                 }
135
136                 public override string ToString ()
137                 {
138                         StringBuilder sb = new StringBuilder (GetType ().FullName);
139                         sb.AppendFormat (": {0}", Message);
140
141                         if (fileName != null && fileName.Length > 0) {
142                                 sb.Append (Environment.NewLine);
143 #if NET_2_0
144                                 sb.AppendFormat ("File name: '{0}'", fileName);
145 #else
146                                 sb.AppendFormat ("File name: \"{0}\"", fileName);
147 #endif
148                         }
149
150                         if (this.InnerException != null)
151                                 sb.AppendFormat (" ---> {0}", InnerException);
152
153                         if (this.StackTrace != null) {
154                                 sb.Append (Environment.NewLine);
155                                 sb.Append (StackTrace);
156                         }
157
158                         return sb.ToString ();
159                 }
160         }
161 }