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