FileNotFoundException.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Runtime.Serialization;
  6. namespace System.IO
  7. {
  8. // Thrown when trying to access a file that doesn't exist on disk.
  9. [Serializable]
  10. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  11. public partial class FileNotFoundException : IOException
  12. {
  13. public FileNotFoundException()
  14. : base(SR.IO_FileNotFound)
  15. {
  16. HResult = HResults.COR_E_FILENOTFOUND;
  17. }
  18. public FileNotFoundException(string? message)
  19. : base(message)
  20. {
  21. HResult = HResults.COR_E_FILENOTFOUND;
  22. }
  23. public FileNotFoundException(string? message, Exception? innerException)
  24. : base(message, innerException)
  25. {
  26. HResult = HResults.COR_E_FILENOTFOUND;
  27. }
  28. public FileNotFoundException(string? message, string? fileName)
  29. : base(message)
  30. {
  31. HResult = HResults.COR_E_FILENOTFOUND;
  32. FileName = fileName;
  33. }
  34. public FileNotFoundException(string? message, string? fileName, Exception? innerException)
  35. : base(message, innerException)
  36. {
  37. HResult = HResults.COR_E_FILENOTFOUND;
  38. FileName = fileName;
  39. }
  40. public override string Message
  41. {
  42. get
  43. {
  44. SetMessageField();
  45. Debug.Assert(_message != null, "_message was null after calling SetMessageField");
  46. return _message;
  47. }
  48. }
  49. private void SetMessageField()
  50. {
  51. if (_message == null)
  52. {
  53. if ((FileName == null) &&
  54. (HResult == System.HResults.COR_E_EXCEPTION))
  55. _message = SR.IO_FileNotFound;
  56. else if (FileName != null)
  57. _message = FileLoadException.FormatFileLoadExceptionMessage(FileName, HResult);
  58. }
  59. }
  60. public string? FileName { get; }
  61. public string? FusionLog { get; }
  62. public override string ToString()
  63. {
  64. string s = GetType().ToString() + ": " + Message;
  65. if (FileName != null && FileName.Length != 0)
  66. s += Environment.NewLine + SR.Format(SR.IO_FileName_Name, FileName);
  67. if (InnerException != null)
  68. s = s + Environment.NewLine + InnerExceptionPrefix + InnerException.ToString();
  69. if (StackTrace != null)
  70. s += Environment.NewLine + StackTrace;
  71. if (FusionLog != null)
  72. {
  73. if (s == null)
  74. s = " ";
  75. s += Environment.NewLine;
  76. s += Environment.NewLine;
  77. s += FusionLog;
  78. }
  79. return s;
  80. }
  81. protected FileNotFoundException(SerializationInfo info, StreamingContext context)
  82. : base(info, context)
  83. {
  84. FileName = info.GetString("FileNotFound_FileName");
  85. FusionLog = info.GetString("FileNotFound_FusionLog");
  86. }
  87. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  88. {
  89. base.GetObjectData(info, context);
  90. info.AddValue("FileNotFound_FileName", FileName, typeof(string));
  91. info.AddValue("FileNotFound_FusionLog", FusionLog, typeof(string));
  92. }
  93. }
  94. }