现在的位置: 首页 > 综合 > 正文

文件操作工具类FileUtility(摘自UABv2.0)

2013年10月08日 ⁄ 综合 ⁄ 共 5714字 ⁄ 字号 评论关闭
最近一直在研究 Smart Client 的 Smart Update 开发,从 Microsoft Updater Application Block v2.0 里面学到了很多东西,这里不得不佩服 Enterprise Library 的设计,设计模式和 XML 的运用使得 Enterprise Library 的扩展性很强,设计十分优美,是学习 OOP 的好范例。本人看了之后感叹自己写的代码大部分还是面向过程
Enterprise Library 的广告就做到这里了,下面一个操作文件的工具类是从 Microsoft Updater Application Block v2.0 里面原封不动取出来,感觉具有一定的参考价值,希望对大家有帮助。

//============================================================================================================

 

// Microsoft Updater Application Block for .NET

 

//  http://msdn.microsoft.com/library/en-us/dnbda/html/updater.asp

 

// 

 

// FileUtility.cs

 

//

 

// Contains the implementation of the FileUtility helper class.

 

//

 

// For more information see the Updater Application Block Implementation Overview.

 

//

 

//============================================================================================================

 

// Copyright ?Microsoft Corporation.  All rights reserved.

 

// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY

 

// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT

 

// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND

 

// FITNESS FOR A PARTICULAR PURPOSE.

 

//============================================================================================================

 

 

 

using System;

 

using System.IO;

 

using System.Runtime.InteropServices;

 

 

 

namespace Microsoft.ApplicationBlocks.Updater.Utilities

 

{

 

    /// <summary>

 

    /// Indicates how to proceed with the move file operation.

 

    /// </summary>

 

    [Flags]

 

    public enum MoveFileFlag : int

 

    {

 

        /// <summary>

 

        /// Perform a default move funtion.

 

        /// </summary>

 

        None                = 0x00000000,

 

        /// <summary>

 

        /// If the target file exists, the move function will replace it.

 

        /// </summary>

 

        ReplaceExisting     = 0x00000001,

 

        /// <summary>

 

        /// If the file is to be moved to a different volume,

 

        /// the function simulates the move by using the CopyFile and DeleteFile functions.

 

        /// </summary>

 

        CopyAllowed         = 0x00000002,

 

        /// <summary>

 

        /// The system does not move the file until the operating system is restarted.

 

        /// The system moves the file immediately after AUTOCHK is executed, but before

 

        /// creating any paging files. Consequently, this parameter enables the function

 

        /// to delete paging files from previous startups.

 

        /// </summary>

 

        DelayUntilReboot    = 0x00000004,

 

        /// <summary>

 

        /// The function does not return until the file has actually been moved on the disk.

 

        /// </summary>

 

        WriteThrough        = 0x00000008,

 

        /// <summary>

 

        /// Reserved for future use.

 

        /// </summary>

 

        CreateHardLink      = 0x00000010,

 

        /// <summary>

 

        /// The function fails if the source file is a link source, but the file cannot be tracked after the move. This situation can occur if the destination is a volume formatted with the FAT file system.

 

        /// </summary>

 

        FailIfNotTrackable  = 0x00000020,

 

    }

 

 

 

    /// <summary>

 

    /// Provides certain utilities used by configuration processors, such as correcting file paths.

 

    /// </summary>

 

    public sealed class FileUtility

 

    {

 

        #region Constructor

 

 

 

        /// <summary>

 

        /// Default constructor.

 

        /// </summary>

 

        private FileUtility()

 

        {

 

        }

 

 

 

        #endregion

 

 

 

        #region Public members

 

 

 

        /// <summary>

 

        /// Returns whether the path is a UNC path.

 

        /// </summary>

 

        /// <param name="path">The path string.</param>

 

        /// <returns><c>true</c> if the path is a UNC path.</returns>

 

        public static bool IsUncPath( string path )

 

        {

 

            //  FIRST, check if this is a URL or a UNC path; do this by attempting to construct uri object from it

 

            Uri url = new Uri( path );

 

                   

 

            if( url.IsUnc )

 

            {

 

                //  it is a unc path, return true

 

                return true;

 

            }

 

            else

 

            {

 

                return false;

 

            }

 

        }

 

 

 

        /// <summary>

 

        /// Takes a UNC or URL path, determines which it is (NOT hardened against bad strings, assumes one or the other is present)

 

        /// and returns the path with correct trailing slash: backslash for UNC or

 

        /// slash mark for URL.

 

        /// </summary>

 

        /// <param name="path">The URL or UNC string.</param>

 

        /// <returns>Path with correct terminal slash.</returns>

 

        public static string AppendSlashUrlOrUnc( string path )

 

        {                  

 

            if( IsUncPath( path ) )

 

            {

 

                //  it is a unc path, so decorate the end with a back-slash (to correct misconfigurations, defend against trivial errors)

 

                return AppendTerminalBackslash( path );

 

            }

 

            else

 

            {

 

                //  assume URL here

 

                return AppendTerminalForwardSlash( path );

 

            }

 

        }

 

 

 

        /// <summary>

 

        /// If not present appends terminal backslash to paths.

 

        /// </summary>

 

        /// <param name="path">A path string; for example, "C:/AppUpdaterClient".</param>

 

        /// <returns>A path string with trailing backslash; for example, "C:/AppUpdaterClient/".</returns>

 

        public static string AppendTerminalBackslash( string path )

 

        {

 

            if( path.IndexOf( Path.DirectorySeparatorChar, path.Length - 1 ) == -1 )

 

            {

 

                return path + Path.DirectorySeparatorChar;

 

            }

 

            else

 

            {

 

                return path;

 

            }

 

        }

 

       

 

        /// <summary>

 

        /// Appends a terminal slash mark if there is not already one; returns corrected path.

 

        /// </summary>

 

        /// <param name="path">The path that may be missing a terminal slash mark.</param>

 

        /// <returns>The corrected path with terminal slash mark.</returns>

 

        public static string AppendTerminalForwardSlash( string path )

 

        {

 

            if( path.IndexOf( Path.AltDirectorySeparatorChar, path.Length - 1 ) == -1 )

 

            {

 

                return path + Path.AltDirectorySeparatorChar;

 

            }

 

            else

 

            {

 

                return path;

 

            }

 

        }

 

 

 

        /// <summary>

 

        ///

抱歉!评论已关闭.