home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipNet.msi / Data1.cab / IsolatedFolder.cs < prev    next >
Encoding:
Text File  |  2002-02-05  |  16.9 KB  |  474 lines

  1. /*
  2.  * Xceed Zip for .NET - ExtendingDemo Sample Classes
  3.  * Copyright (c) 2000-2002 - Xceed Software Inc.
  4.  * 
  5.  * [IsolatedFolder.cs]
  6.  * 
  7.  * This application demonstrates how to extend the Xceed FileSystem object model
  8.  * by deriving from the AbstractFolder class.
  9.  * 
  10.  * This file is part of Xceed Zip for .NET. The source code in this file 
  11.  * is only intended as a supplement to the documentation, and is provided 
  12.  * "as is", without warranty of any kind, either expressed or implied.
  13.  */
  14.  
  15. using System;
  16. using System.Collections;
  17. using System.IO;
  18. using System.IO.IsolatedStorage;
  19. using Xceed.FileSystem;
  20.  
  21. namespace Xceed.FileSystem.Samples.ExtendingDemo
  22. {
  23.     /// <summary>
  24.   /// This class demonstrates how to create a class that derives
  25.   /// from the <see cref="AbstractFolder"/> class and handles isolated storage folders. 
  26.   /// </summary>
  27.     public class IsolatedFolder : AbstractFolder
  28.     {    
  29.     #region PUBLIC CONSTRUCTORS
  30.       
  31.     /// <summary>
  32.     /// Creates a new instance of IsolatedFolder class.
  33.     /// </summary>
  34.     /// <param name="folderName">Name (with path) of the folder.</param>
  35.     /// <remarks><para>Allowed path: "foo", "foo\bar", "\foo\bar"</para></remarks>
  36.     public IsolatedFolder( string folderName )
  37.       {
  38.       if( folderName == null )
  39.         throw new ArgumentNullException( "folderName" );
  40.  
  41.       folderName = folderName.Trim();
  42.  
  43.       if( folderName.IndexOfAny( this.InvalidChars ) != -1 )
  44.         throw new ArgumentException( "The folder name contains invalid characters.", "folderName" );
  45.  
  46.       folderName = folderName.Replace( Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar );
  47.       
  48.       folderName = folderName.StartsWith( Path.DirectorySeparatorChar.ToString() ) ? 
  49.         folderName : ( Path.DirectorySeparatorChar + folderName );
  50.         
  51.       folderName = folderName.EndsWith( Path.DirectorySeparatorChar.ToString() ) ? 
  52.         folderName : ( folderName + Path.DirectorySeparatorChar );
  53.         
  54.       m_fullName = folderName;
  55.       
  56.       if( m_fullName == Path.DirectorySeparatorChar.ToString() )
  57.       {
  58.         m_name   = string.Empty;
  59.         m_isRoot = true;
  60.       }
  61.       else
  62.       {
  63.         m_name   = Path.GetFileName( m_fullName.TrimEnd( Path.DirectorySeparatorChar ) );
  64.       }
  65.     }
  66.       
  67.     #endregion PUBLIC CONSTRUCTORS
  68.  
  69.     #region PRIVATE METHODS
  70.  
  71.     /// <summary>
  72.     /// Retrieves an array of <see cref="FileSystemItem"/> objects representing each
  73.     /// item ( file and folder ) within the isolated storage folder.
  74.     /// </summary>
  75.     /// <param name="session">A reference to a <see cref="FileSystemEventsSession"/> object which is 
  76.     /// responsible for raising all events that occur during the process.</param>
  77.     /// <returns>An array of <see cref="FileSystemItem"/> objects representing each item 
  78.     /// ( file and folder ) within the isolated storage folder.</returns>
  79.     protected override FileSystemItem[] DoGetChildItems( FileSystemEventsSession session )
  80.     {
  81.       try
  82.       {
  83.         FileSystemItem[] childItems;
  84.         
  85.         IsolatedStorageFile isoStore = IsolatedFolder.m_domainStore;
  86.  
  87.         string[] childFiles = isoStore.GetFileNames( this.FullName + "*" );
  88.         string[] childFolders = isoStore.GetDirectoryNames( this.FullName + "*" );
  89.         
  90.         childItems = new FileSystemItem[ childFolders.Length + childFiles.Length ];
  91.         
  92.         for( int i = 0; i < childFolders.Length; i++ )
  93.         {
  94.           childItems[ i ] = new IsolatedFolder( this.FullName + childFolders[ i ] );
  95.         }            
  96.  
  97.         for( int i = 0; i < childFiles.Length; i++ )
  98.         {
  99.           childItems[ childFolders.Length + i ] = new IsolatedFile( this.FullName + childFiles[ i ] );
  100.         }            
  101.  
  102.         return childItems;
  103.       }
  104.       catch( IsolatedStorageException except )
  105.       {
  106.         throw new FileSystemException( "The list of child items could not be retrieved from the folder.", this, except );
  107.       }
  108.     }
  109.  
  110.     /// <summary>
  111.     /// Gets or sets the short name of the folder
  112.     /// </summary>
  113.     /// <value>A string that represents the short name of the folder.</value>
  114.     /// <remarks><para>The short name consists of the item's name without a path. 
  115.     /// For example, the short name of a folder named "c:\foo\" is "foo". /para></remarks>
  116.     protected override string DoName
  117.     {
  118.       get
  119.       {
  120.         return m_name;
  121.       }
  122.       set
  123.       {
  124.         if( value == this.Name )
  125.           return;
  126.         
  127.         if( this.Exists )
  128.         {
  129.           // Renaming a folder in the isolated storage isn't that simple.
  130.           // We must create a new folder with the new name, move each 
  131.           // child file and folder into that new foldera and then delete the
  132.           // original folder.
  133.           string path = this.ParentFolder.FullName;
  134.             
  135.           IsolatedFolder folder = new IsolatedFolder( path + value );
  136.           folder.Create();
  137.           
  138.           foreach( FileSystemItem item in this.GetItems( false ) )
  139.           {
  140.             item.MoveTo( folder, true );
  141.           }
  142.  
  143.           this.Delete();
  144.  
  145.           m_fullName = folder.FullName;
  146.           m_name = folder.Name;
  147.         }
  148.         else
  149.         {
  150.           m_fullName = this.ParentFolder.FullName + value + Path.DirectorySeparatorChar;
  151.           m_name = value;
  152.         }
  153.       }
  154.     }
  155.  
  156.     /// <summary>
  157.     /// Gets the full name of the folder
  158.     /// </summary>
  159.     /// <value>A string that contains the full name of the folder.</value>
  160.     /// <remarks><para>The full name consists of the item's name fully-qualified 
  161.     /// with a path. For example: "c:\foo\".</para></remarks>    
  162.     protected override string DoFullName
  163.     {
  164.       get
  165.       {
  166.         return m_fullName;
  167.       }
  168.     }
  169.  
  170.     /// <summary>
  171.     /// Folders in isolated storage do have attributes. Therefore, we throw
  172.     /// a <see cref="FileSystemNotSupportException"/> when trying to get or set the folder attributes.
  173.     /// </summary>
  174.     /// <returns><see cref="FileSystemNotSupportException"/></returns>    
  175.     protected override FileAttributes DoAttributes
  176.     {
  177.       get
  178.       {
  179.         throw new FileSystemNotSupportedException( this );
  180.       }
  181.       set
  182.       {
  183.         throw new FileSystemNotSupportedException( this );
  184.       }
  185.     }
  186.  
  187.     /// <summary>
  188.     /// Since there is no notion of date/time when dealing with folders in 
  189.     /// isolate storage, we throw a <see cref="FileSystemNotSupportException"/> when trying
  190.     /// to get or set the CreationDateTime.
  191.     /// </summary>
  192.     /// <returns><see cref="FileSystemNotSupportException"/></returns>
  193.     protected override DateTime DoCreationDateTime
  194.     {
  195.       get
  196.       {
  197.         throw new FileSystemNotSupportedException( this );
  198.       }
  199.       set
  200.       {
  201.         throw new FileSystemNotSupportedException( this );
  202.       }
  203.     }
  204.  
  205.     /// <summary>
  206.     /// Since there is no notion of date/time when dealing with folders in 
  207.     /// isolate storage, we throw a <see cref="FileSystemNotSupportException"/> when trying
  208.     /// to get or set the LastWriteDateTime.
  209.     /// </summary>
  210.     /// <returns><see cref="FileSystemNotSupportException"/></returns>
  211.     protected override DateTime DoLastWriteDateTime
  212.     {
  213.       get
  214.       {
  215.         throw new FileSystemNotSupportedException( this );
  216.       }
  217.       set
  218.       {
  219.         throw new FileSystemNotSupportedException( this );
  220.       }
  221.     }
  222.  
  223.     /// <summary>
  224.     /// Since there is no notion of date/time when dealing with folders in 
  225.     /// isolate storage, we throw a <see cref="FileSystemNotSupportException"/> when trying
  226.     /// to get or set the LastAccessDateTime.
  227.     /// </summary>
  228.     /// <returns><see cref="FileSystemNotSupportException"/></returns>
  229.     protected override DateTime DoLastAccessDateTime
  230.     {
  231.       get
  232.       {
  233.         throw new FileSystemNotSupportedException( this );
  234.       }
  235.       set
  236.       {
  237.         throw new FileSystemNotSupportedException( this );
  238.       }
  239.     }
  240.  
  241.     /// <summary>
  242.     /// Gets a reference to the parent folder of the folder.
  243.     /// </summary>
  244.     /// <returns>A reference to an IsolatedFolder object that represents the folder which 
  245.     /// contains the folder, or a null reference if the folder does not have a parent.</returns>
  246.     protected override AbstractFolder DoParentFolder
  247.     {
  248.       get
  249.       {
  250.         string directoryName = this.FullName.TrimEnd( Path.DirectorySeparatorChar );
  251.         
  252.         if( directoryName.Length != 0 )
  253.         {
  254.           string parentFullName = Path.GetDirectoryName( directoryName );
  255.  
  256.           if( ( parentFullName != null ) && ( parentFullName.Length != 0 ) )
  257.           {
  258.             return new IsolatedFolder( parentFullName );
  259.           }
  260.         }
  261.  
  262.         return null;
  263.       }
  264.     }
  265.  
  266.     /// <summary>
  267.     /// Gets a reference to the root folder of the folder.
  268.     /// </summary>
  269.     /// <returns>A reference to an IsolatedFolder object that represents
  270.     /// the root folder of the folder.</returns>
  271.     protected override AbstractFolder DoRootFolder
  272.     {
  273.       get
  274.       {
  275.         return new IsolatedFolder( string.Empty );
  276.       }
  277.     }
  278.     
  279.     /// <summary>
  280.     /// Gets a boolean value indicating if the folder physically exists.
  281.     /// </summary>
  282.     /// <returns><see langword="true"/> if the item physically exists; <see langword="false"/> otherwise.</returns>
  283.     protected override bool DoExists
  284.     {
  285.       get
  286.       {
  287.         if( this.IsRoot )
  288.           return true;
  289.  
  290.         try
  291.         {
  292.           string directoryName = this.FullName.TrimEnd( Path.DirectorySeparatorChar );
  293.  
  294.           IsolatedStorageFile isoStore = IsolatedFolder.m_domainStore;
  295.  
  296.           return ( isoStore.GetDirectoryNames( directoryName ).Length != 0 );
  297.         }
  298.         catch( DirectoryNotFoundException )
  299.         {
  300.           return false;
  301.         }
  302.         catch( IsolatedStorageException )
  303.         {
  304.           return false;
  305.         }
  306.       }
  307.     }
  308.     
  309.     /// <summary>
  310.     /// Physically created the folder in the isolated storage.
  311.     /// </summary>
  312.     /// <param name="session">A reference to a <see cref="FileSystemEventsSession"/> object which is 
  313.     /// responsible for raising all events that occur during the process.</param>
  314.     protected override void DoCreate( FileSystemEventsSession session )
  315.     {
  316.       if( this.Exists )
  317.         throw new ItemAlreadyExistsException( this );
  318.  
  319.       // The public Create method is responsible for making sure our parent folder
  320.       // exists. The DoCreate method can assume it's parents exist. We still verify if they do
  321.       // in case something real bad happened.
  322.       if( this.ParentFolder != null && !this.ParentFolder.Exists )
  323.       {
  324.         System.Diagnostics.Debug.Assert( false );
  325.         throw new FileSystemInternalException( "An unexpected attempt was made to create an item in a folder that does not exist.", this );
  326.       }
  327.  
  328.       try
  329.       {
  330.         string directoryName = this.FullName.TrimEnd( Path.DirectorySeparatorChar );
  331.         
  332.         IsolatedStorageFile isoStore = IsolatedFolder.m_domainStore;
  333.  
  334.         isoStore.CreateDirectory( directoryName );
  335.       }
  336.       catch( IsolatedStorageException except )
  337.       {
  338.         throw new FileSystemException( "The physical folder represented by the item could not be created.", this, except );
  339.       }
  340.     }
  341.  
  342.     /// <summary>
  343.     /// Re-reads the information from the physical folder.
  344.     /// </summary>
  345.     protected override void DoRefresh( FileSystemEventsSession session )
  346.     {
  347.       if( !this.Exists )
  348.         throw new ItemDoesNotExistException( this );
  349.  
  350.       // We have nothing to refresh!
  351.     }
  352.     
  353.     /// <summary>
  354.     /// Retrieves a reference to an <see cref="AbstractFile"/> object representing 
  355.     /// a file in the isolated store.
  356.     /// </summary>
  357.     /// <param name="session">A reference to a <see cref="FileSystemEventsSession"/> object which is 
  358.     /// responsible for raising all events that occur during the process.</param>
  359.     /// <param name="fileName">The name of the file to retrieve.</param>
  360.     /// <returns>A reference to an <see cref="AbstractFile"/> object regardless of if the file represented by the object exists or not.
  361.     /// </returns>
  362.     /// <remarks><para><paramref name="fileName"/> should not end with a <see cref="System.IO.Path.DirectorySeparatorChar"/>.
  363.     /// </para></remarks>
  364.     protected override AbstractFile DoGetFile( FileSystemEventsSession session, string fileName )
  365.     {
  366.       System.Diagnostics.Debug.Assert( 
  367.         fileName.IndexOf( Path.DirectorySeparatorChar ) == -1, 
  368.         "The fileName parameter cannot end with a directory separator char." );
  369.     
  370.       return new IsolatedFile( this.FullName + fileName );
  371.     }
  372.  
  373.     /// <summary>
  374.     /// Retrieves a reference to an <see cref="AbstractFolder"/> object representing 
  375.     /// a folder in the isolated store.
  376.     /// </summary>
  377.     /// <param name="session">A reference to a <see cref="FileSystemEventsSession"/> object which is 
  378.     /// responsible for raising all events that occur during the process.</param>
  379.     /// <param name="folderName">The name of the folder to retrieve.</param>
  380.     /// <returns>A reference to an <see cref="AbstractFolder"/> object regardless of if the folder represented by the object exists or not.</returns>
  381.     /// <remarks><para><paramref name="folderName"/> should not end with a <see cref="System.IO.Path.DirectorySeparatorChar"/>.
  382.     /// </para></remarks>
  383.     protected override AbstractFolder DoGetFolder( FileSystemEventsSession session, string folderName )
  384.     {
  385.       System.Diagnostics.Debug.Assert( 
  386.         folderName.IndexOf( Path.DirectorySeparatorChar ) == -1, 
  387.         "The folderName parameter cannot end with a directory separator char." );
  388.     
  389.       return new IsolatedFolder( this.FullName + folderName );
  390.     }
  391.     
  392.     /// <summary>
  393.     /// Gets a boolean value indicating if the source and target items are the same.
  394.     /// </summary>
  395.     /// <param name="target">A <see cref="FileSystemItem"/> object representing the destination folder.</param>
  396.     /// <returns><see langword="true"/> if the source and target <see cref="FileSystemItem"/> objects are the same; <see langword="false"/> otherwise.</returns>
  397.     protected override bool IsSameAs( FileSystemItem target )
  398.     {
  399.       if( target == null )
  400.         return false;
  401.         
  402.       // In some file systems, two identical folder names does not mean
  403.       // it's the same folder. With the isolated storage's domain store,
  404.       // we can simply check if both items have the same type and fullname.
  405.       if( target is IsolatedFolder )
  406.       {
  407.         return( this.FullName == target.FullName );
  408.       }
  409.       else
  410.       {
  411.         return false;
  412.       }
  413.     }
  414.     
  415.     /// <summary>
  416.     /// Indicates if the provided path contains a root description.
  417.     /// </summary>
  418.     /// <param name="path">The path to verify</param>
  419.     /// <returns><see langword="true"/> if the path is rooted; <see langword="false"/> otherwise.</returns>
  420.     protected override bool IsPathRooted( string path )
  421.     {
  422.       // With Isolated Storage, there is no such thing as a rooted path. 
  423.       // For example, "D:\Folder" is a rooted path for disk items.
  424.       return false;
  425.     }
  426.     
  427.     /// <summary>
  428.     /// Permanently deletes the file.
  429.     /// </summary>
  430.     /// <param name="session">A reference to a <see cref="FileSystemEventsSession"/> object which is responsible for raising all 
  431.     /// events that occur during the process.</param>
  432.     protected override void DoDelete( FileSystemEventsSession session )
  433.     {
  434.       if( !this.Exists )
  435.         throw new ItemDoesNotExistException( this );
  436.  
  437.       // The public Delete method is responsible for calling Delete on all
  438.       // children. We simply need to delete ourself. If we're not empty, it
  439.       // means one or more child items were ignored in the OnItemException event.
  440.       try
  441.       {
  442.         string directoryName = this.FullName.TrimEnd( Path.DirectorySeparatorChar );
  443.         
  444.         IsolatedStorageFile isoStore = IsolatedFolder.m_domainStore;
  445.  
  446.         isoStore.DeleteDirectory( directoryName );
  447.       }
  448.       catch( IsolatedStorageException except )
  449.       {
  450.         throw new FileSystemException( "The physical folder represented by the item could not be deleted.", this, except );
  451.       }
  452.     }
  453.  
  454.     #endregion PRIVATE METHODS
  455.  
  456.     #region PRIVATE FIELDS
  457.         
  458.     /// <summary>The name of the folder without it's parent's path.</summary>
  459.     private string m_name     = string.Empty;
  460.  
  461.     /// <summary>The fully qualified name of the folder.</summary>
  462.     private string m_fullName = string.Empty;
  463.     
  464.         #endregion PRIVATE FIELDS
  465.         
  466.     #region INTERNAL STATIC FIELDS
  467.     
  468.     /// <summary>This implementation only supports the User Store.</summary>
  469.     internal static IsolatedStorageFile m_domainStore = IsolatedStorageFile.GetUserStoreForDomain();
  470.     
  471.     #endregion STATIC PUBLIC PROPERTIES
  472.   }
  473. }
  474.