2008-09-14 Zoltan Varga <vargaz@gmail.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                 string fileName;
53                 string fusionLog;
54                 
55                 // Constructors
56                 public FileLoadException ()
57                         : base (Locale.GetText ("I/O Error"))
58                 {
59                         HResult = Result;
60                         msg = Locale.GetText ("I/O Error");
61                 }
62
63                 public FileLoadException (string message)
64                         : base (message)
65                 {
66                         HResult = Result;
67                         msg = message;
68                 }
69
70                 public FileLoadException (string message, string fileName)
71                         : base (message)
72                 {
73                         HResult = Result;
74                         this.msg = message;
75                         this.fileName = fileName;
76                 }               
77
78                 public FileLoadException (string message, Exception inner)
79                         : base (message, inner)
80                 {
81                         HResult = Result;
82                         msg = message;
83                 }
84
85                 public FileLoadException (string message, string fileName, Exception inner)
86                         : base (message, inner)
87                 {
88                         HResult = Result;
89                         this.msg = message;
90                         this.fileName = fileName;
91                 }
92
93                 protected FileLoadException (SerializationInfo info, StreamingContext context)
94                 {
95                         fileName = info.GetString ("FileLoad_FileName");
96                         fusionLog = info.GetString ("FileLoad_FusionLog");
97                 }
98
99                 // Properties
100                 public override string Message {
101                         get { return msg; }
102                 }
103
104                 public string FileName
105                 {
106                         get { return fileName; }
107                 }
108                 
109                 public string FusionLog {
110                         // note: MS runtime throws a SecurityException when the Exception is created
111                         // but a FileLoadException once the exception as been thrown. Mono always
112                         // throw a SecurityException in both case (anyway fusionLog is currently empty)
113                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
114                         get { return fusionLog; }
115                 }
116
117                 // Methods
118                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
119                 {
120                         base.GetObjectData (info, context);
121                         info.AddValue ("FileLoad_FileName", fileName);
122                         info.AddValue ("FileLoad_FusionLog", fusionLog);
123                 }
124
125                 public override string ToString ()
126                 {
127                         StringBuilder sb = new StringBuilder (GetType ().FullName);
128                         sb.AppendFormat (": {0}", msg);
129
130                         if (fileName != null)
131                                 sb.AppendFormat (" : {0}", fileName);
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 }