FileNotFoundException.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // System.IO.FileNotFoundException.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  6. // Duncan Mak ([email protected])
  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. using System.Globalization;
  31. using System.Runtime.Serialization;
  32. using System.Security.Permissions;
  33. using System.Text;
  34. namespace System.IO {
  35. [Serializable]
  36. public class FileNotFoundException : IOException {
  37. const int Result = unchecked ((int)0x80131621);
  38. private string fileName;
  39. private string fusionLog;
  40. // Constructors
  41. public FileNotFoundException ()
  42. : base (Locale.GetText ("Unable to find the specified file."))
  43. {
  44. HResult = Result;
  45. }
  46. public FileNotFoundException (string message)
  47. : base (message)
  48. {
  49. HResult = Result;
  50. }
  51. public FileNotFoundException (string message, Exception inner)
  52. : base (message, inner)
  53. {
  54. HResult = Result;
  55. }
  56. public FileNotFoundException (string message, string fileName)
  57. : base (message)
  58. {
  59. HResult = Result;
  60. this.fileName = fileName;
  61. }
  62. public FileNotFoundException (string message, string fileName, Exception innerException)
  63. : base (message, innerException)
  64. {
  65. HResult = Result;
  66. this.fileName = fileName;
  67. }
  68. protected FileNotFoundException (SerializationInfo info, StreamingContext context)
  69. : base (info, context)
  70. {
  71. fileName = info.GetString ("FileNotFound_FileName");
  72. fusionLog = info.GetString ("FileNotFound_FusionLog");
  73. }
  74. public string FileName
  75. {
  76. get { return fileName; }
  77. }
  78. public string FusionLog {
  79. // note: MS runtime throws a SecurityException when the Exception is created
  80. // but a FileLoadException once the exception as been thrown. Mono always
  81. // throw a SecurityException in both case (anyway fusionLog is currently empty)
  82. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  83. get { return fusionLog; }
  84. }
  85. public override string Message {
  86. get {
  87. if (base.message == null) {
  88. #if NET_2_0
  89. if (fileName != null) {
  90. string message = string.Format (CultureInfo.CurrentCulture,
  91. "Could not load file or assembly '{0}' or one of"
  92. + " its dependencies. The system cannot find the"
  93. + " file specified.", fileName);
  94. return message;
  95. }
  96. #else
  97. string file = fileName == null ? "(null)" : fileName;
  98. string message = string.Format (CultureInfo.CurrentCulture,
  99. "File or assembly name {0}, or one of its dependencies,"
  100. + " was not found.", file);
  101. return message;
  102. #endif
  103. }
  104. return base.message;
  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. public override string ToString ()
  114. {
  115. StringBuilder sb = new StringBuilder (GetType ().FullName);
  116. sb.AppendFormat (": {0}", Message);
  117. if (fileName != null && fileName.Length > 0) {
  118. sb.Append (Environment.NewLine);
  119. #if NET_2_0
  120. sb.AppendFormat ("File name: '{0}'", fileName);
  121. #else
  122. sb.AppendFormat ("File name: \"{0}\"", fileName);
  123. #endif
  124. }
  125. if (this.InnerException != null)
  126. sb.AppendFormat (" ---> {0}", InnerException);
  127. if (this.StackTrace != null) {
  128. sb.Append (Environment.NewLine);
  129. sb.Append (StackTrace);
  130. }
  131. return sb.ToString ();
  132. }
  133. }
  134. }