The Labs \ Source Viewer \ SSCLI \ System.IO \ FileNotFoundException

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  FileNotFoundException
  18. **
  19. **
  20. ** Purpose: Exception for accessing a file that doesn't exist.
  21. **
  22. **
  23. ===========================================================*/
  24. using System;
  25. using System.Runtime.Serialization;
  26. using System.Security.Permissions;
  27. using SecurityException = System.Security.SecurityException;
  28. using System.Globalization;
  29. namespace System.IO
  30. {
  31.     // Thrown when trying to access a file that doesn't exist on disk.
  32.     [Serializable()]
  33.     [System.Runtime.InteropServices.ComVisible(true)]
  34.     public class FileNotFoundException : IOException
  35.     {
  36.        
  37.         private string _fileName;
  38.         // The name of the file that isn't found.
  39.         private string _fusionLog;
  40.         // fusion log (when applicable)
  41.         public FileNotFoundException() : base(Environment.GetResourceString("IO.FileNotFound"))
  42.         {
  43.             SetErrorCode(__HResults.COR_E_FILENOTFOUND);
  44.         }
  45.        
  46.         public FileNotFoundException(string message) : base(message)
  47.         {
  48.             SetErrorCode(__HResults.COR_E_FILENOTFOUND);
  49.         }
  50.        
  51.         public FileNotFoundException(string message, Exception innerException) : base(message, innerException)
  52.         {
  53.             SetErrorCode(__HResults.COR_E_FILENOTFOUND);
  54.         }
  55.        
  56.         public FileNotFoundException(string message, string fileName) : base(message)
  57.         {
  58.             SetErrorCode(__HResults.COR_E_FILENOTFOUND);
  59.             _fileName = fileName;
  60.         }
  61.        
  62.         public FileNotFoundException(string message, string fileName, Exception innerException) : base(message, innerException)
  63.         {
  64.             SetErrorCode(__HResults.COR_E_FILENOTFOUND);
  65.             _fileName = fileName;
  66.         }
  67.        
  68.         public override string Message {
  69.             get {
  70.                 SetMessageField();
  71.                 return _message;
  72.             }
  73.         }
  74.        
  75.         private void SetMessageField()
  76.         {
  77.             if (_message == null) {
  78.                 if ((_fileName == null) && (HResult == System.__HResults.COR_E_EXCEPTION))
  79.                     _message = Environment.GetResourceString("IO.FileNotFound");
  80.                 else if (_fileName != null)
  81.                    
  82.                     _message = FileLoadException.FormatFileLoadExceptionMessage(_fileName, HResult);
  83.             }
  84.         }
  85.        
  86.         public string FileName {
  87.             get { return _fileName; }
  88.         }
  89.        
  90.         public override string ToString()
  91.         {
  92.             string s = GetType().FullName + ": " + Message;
  93.            
  94.             if (_fileName != null && _fileName.Length != 0)
  95.                 s += Environment.NewLine + String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.FileName_Name"), _fileName);
  96.            
  97.             if (InnerException != null)
  98.                 s = s + " ---> " + InnerException.ToString();
  99.            
  100.             if (StackTrace != null)
  101.                 s += Environment.NewLine + StackTrace;
  102.            
  103.             try {
  104.                 if (FusionLog != null) {
  105.                     if (s == null)
  106.                         s = " ";
  107.                     s += Environment.NewLine;
  108.                     s += Environment.NewLine;
  109.                     s += FusionLog;
  110.                 }
  111.             }
  112.             catch (SecurityException) {
  113.                
  114.             }
  115.             return s;
  116.         }
  117.        
  118.         protected FileNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context)
  119.         {
  120.             // Base class constructor will check info != null.
  121.            
  122.             _fileName = info.GetString("FileNotFound_FileName");
  123.             try {
  124.                 _fusionLog = info.GetString("FileNotFound_FusionLog");
  125.             }
  126.             catch {
  127.                 _fusionLog = null;
  128.             }
  129.            
  130.         }
  131.        
  132.         private FileNotFoundException(string fileName, string fusionLog, int hResult) : base(null)
  133.         {
  134.             SetErrorCode(hResult);
  135.             _fileName = fileName;
  136.             _fusionLog = fusionLog;
  137.             SetMessageField();
  138.         }
  139.        
  140.         public string FusionLog {
  141.             [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlEvidence | SecurityPermissionFlag.ControlPolicy)]
  142.             get { return _fusionLog; }
  143.         }
  144.        
  145.        
  146.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  147.         public override void GetObjectData(SerializationInfo info, StreamingContext context)
  148.         {
  149.             // Serialize data for our base classes. base will verify info != null.
  150.             base.GetObjectData(info, context);
  151.            
  152.             // Serialize data for this class
  153.             info.AddValue("FileNotFound_FileName", _fileName, typeof(string));
  154.            
  155.             try {
  156.                 info.AddValue("FileNotFound_FusionLog", FusionLog, typeof(string));
  157.             }
  158.             catch (SecurityException) {
  159.             }
  160.         }
  161.     }
  162. }

Developer Fusion