2005-08-11 Atsushi Enomoto <atsushi@ximian.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 ("File not found"))
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                                         return "File not found";
103                                 return base.Message;
104                         }
105                 }
106
107                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
108                 {
109                         base.GetObjectData (info, context);
110                         info.AddValue ("FileNotFound_FileName", fileName);
111                         info.AddValue ("FileNotFound_FusionLog", fusionLog);
112                 }
113
114                 public override string ToString ()
115                 {
116                         StringBuilder sb = new StringBuilder (GetType ().FullName);
117                         sb.AppendFormat (": {0}", Message);
118
119                         if (fileName != null)
120                                 sb.AppendFormat (" : {0}", fileName);
121
122                         if (this.InnerException != null)
123                                 sb.AppendFormat (" ----> {0}", InnerException);
124
125                         if (this.StackTrace != null) {
126                                 sb.Append (Environment.NewLine);
127                                 sb.Append (StackTrace);
128                         }
129
130                         return sb.ToString ();
131                 }
132         }
133 }