FileNotFoundException.cs 3.4 KB

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