示例#1
0
        private static IList <MoveResult> MoveDirectorySafe(Microsoft.Expression.Project.MoveInfo moveRequest, bool deleteSource, bool shouldOverwrite)
        {
            if (!Microsoft.Expression.Framework.Documents.PathHelper.IsDirectory(moveRequest.Source) || !Microsoft.Expression.Framework.Documents.PathHelper.IsDirectory(moveRequest.Destination))
            {
                throw new ArgumentException("Request must be a directory", "moveRequest");
            }
            Microsoft.Expression.Framework.Documents.PathHelper.ClearFileOrDirectoryReadOnlyAttribute(moveRequest.Source);
            string             source      = moveRequest.Source;
            string             destination = moveRequest.Destination;
            IList <MoveResult> moveResults = new List <MoveResult>();
            int length = source.Length;

            string[] files = Directory.GetFiles(source, "*", SearchOption.AllDirectories);
            for (int i = 0; i < (int)files.Length; i++)
            {
                string str = files[i];
                Microsoft.Expression.Framework.Documents.PathHelper.ClearFileOrDirectoryReadOnlyAttribute(str);
                string str1          = Microsoft.Expression.Framework.Documents.PathHelper.ResolveCombinedPath(destination, str.Substring(length));
                string directoryName = Path.GetDirectoryName(str1);
                if (!Microsoft.Expression.Framework.Documents.PathHelper.FileOrDirectoryExists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }
                bool flag = Microsoft.Expression.Framework.Documents.PathHelper.FileOrDirectoryExists(str1);
                if (!flag || shouldOverwrite)
                {
                    if (flag)
                    {
                        Microsoft.Expression.Framework.Documents.PathHelper.ClearFileOrDirectoryReadOnlyAttribute(str1);
                        File.Delete(str1);
                    }
                    File.Move(str, str1);
                    MoveResult moveResult = new MoveResult()
                    {
                        Source            = str,
                        Destination       = str1,
                        MovedSuccessfully = true
                    };
                    moveResults.Add(moveResult);
                }
            }
            IEnumerable <string> strs = Directory.EnumerateDirectories(source, "*", SearchOption.AllDirectories).AppendItem <string>(source);

            foreach (string str2 in strs)
            {
                string str3 = Microsoft.Expression.Framework.Documents.PathHelper.ResolveCombinedPath(destination, str2.Substring(length));
                if (Microsoft.Expression.Framework.Documents.PathHelper.FileOrDirectoryExists(str3))
                {
                    continue;
                }
                Directory.CreateDirectory(str3);
            }
            try
            {
                if (deleteSource)
                {
                    Directory.Delete(source, true);
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                if ((exception is IOException || exception is UnauthorizedAccessException) && (new DirectoryInfo(source)).Exists && ((int)Directory.GetFiles(source, "*", SearchOption.AllDirectories).Length != 0 || (int)Directory.GetDirectories(source, "*", SearchOption.AllDirectories).Length != 0))
                {
                    throw;
                }
            }
            MoveResult moveResult1 = new MoveResult()
            {
                Destination       = moveRequest.Destination,
                Source            = moveRequest.Source,
                MovedSuccessfully = true
            };

            moveResults.Add(moveResult1);
            return(moveResults);
        }