FileNotFoundException.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 (!string.IsNullOrEmpty(FileName))
  66. s += Environment.NewLineConst + SR.Format(SR.IO_FileName_Name, FileName);
  67. if (InnerException != null)
  68. s += Environment.NewLineConst + InnerExceptionPrefix + InnerException.ToString();
  69. if (StackTrace != null)
  70. s += Environment.NewLineConst + StackTrace;
  71. if (FusionLog != null)
  72. {
  73. s ??= " ";
  74. s += Environment.NewLineConst + Environment.NewLineConst + FusionLog;
  75. }
  76. return s;
  77. }
  78. protected FileNotFoundException(SerializationInfo info, StreamingContext context)
  79. : base(info, context)
  80. {
  81. FileName = info.GetString("FileNotFound_FileName");
  82. FusionLog = info.GetString("FileNotFound_FusionLog");
  83. }
  84. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  85. {
  86. base.GetObjectData(info, context);
  87. info.AddValue("FileNotFound_FileName", FileName, typeof(string));
  88. info.AddValue("FileNotFound_FusionLog", FusionLog, typeof(string));
  89. }
  90. }
  91. }