FileNotFoundException.cs 4.5 KB

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