FileNotFoundException.cs 4.1 KB

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