2007-04-30 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / FileLoadException.cs
1 //
2 // System.IO.FileLoadException.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;
34 using System.Security.Permissions;
35 using System.Text;
36
37 #if NET_2_0
38 using System.Runtime.InteropServices;
39 #endif
40
41 namespace System.IO {
42
43         [Serializable]
44 #if NET_2_0
45         [ComVisible (true)]
46 #endif
47         public class FileLoadException : IOException {
48
49                 // Fields
50                 const int Result = unchecked ((int)0x80070002);
51                 string msg;
52                 Exception inner;
53                 string fileName;
54                 string fusionLog;
55                 
56                 // Constructors
57                 public FileLoadException ()
58                         : base (Locale.GetText ("I/O Error"))
59                 {
60                         HResult = Result;
61                         msg = Locale.GetText ("I/O Error");
62                 }
63
64                 public FileLoadException (string message)
65                         : base (message)
66                 {
67                         HResult = Result;
68                         msg = message;
69                 }
70
71                 public FileLoadException (string message, string fileName)
72                         : base (message)
73                 {
74                         HResult = Result;
75                         this.msg = message;
76                         this.fileName = fileName;
77                 }               
78
79                 public FileLoadException (string message, Exception inner)
80                         : base (message, inner)
81                 {
82                         HResult = Result;
83                         msg = message;
84                         this.inner = inner;
85                 }
86
87                 public FileLoadException (string message, string fileName, Exception inner)
88                         : base (message, inner)
89                 {
90                         HResult = Result;
91                         this.msg = message;
92                         this.fileName = fileName;
93                         this.inner = inner;
94                 }
95
96                 protected FileLoadException (SerializationInfo info, StreamingContext context)
97                 {
98                         fileName = info.GetString ("FileLoad_FileName");
99                         fusionLog = info.GetString ("FileLoad_FusionLog");
100                 }
101
102                 // Properties
103                 public override string Message {
104                         get { return msg; }
105                 }
106
107                 public string FileName
108                 {
109                         get { return fileName; }
110                 }
111                 
112                 public string FusionLog {
113                         // note: MS runtime throws a SecurityException when the Exception is created
114                         // but a FileLoadException once the exception as been thrown. Mono always
115                         // throw a SecurityException in both case (anyway fusionLog is currently empty)
116                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
117                         get { return fusionLog; }
118                 }
119
120                 // Methods
121                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
122                 {
123                         base.GetObjectData (info, context);
124                         info.AddValue ("FileLoad_FileName", fileName);
125                         info.AddValue ("FileLoad_FusionLog", fusionLog);
126                 }
127
128                 public override string ToString ()
129                 {
130                         StringBuilder sb = new StringBuilder (GetType ().FullName);
131                         sb.AppendFormat (": {0}", msg);
132
133                         if (fileName != null)
134                                 sb.AppendFormat (" : {0}", fileName);
135
136                         if (this.InnerException != null)
137                                 sb.AppendFormat (" ----> {0}", InnerException);
138
139                         if (this.StackTrace != null) {
140                                 sb.Append (Environment.NewLine);
141                                 sb.Append (StackTrace);
142                         }
143
144                         return sb.ToString ();
145                 }
146         }
147 }