コード例 #1
0
 public static bool Exists(string path)
 {
     return(PathUtil.GetPathState(path) == PathState.File);
 }
コード例 #2
0
        /// <summary>
        /// 拷贝源路径到目标路径
        /// </summary>
        /// <param name="sourPath"> 源路径 </param>
        /// <param name="destPath"> 目标路径 </param>
        /// <param name="force"> 是否强制覆盖目标路径 </param>
        public static void CopyPath(string sourPath, string destPath, bool force)
        {
            if (sourPath == destPath)
            {
                throw new DoodleException("sourPath and destPath can't be same!");
            }


            int    wildcardIndex = sourPath.LastIndexOf("*", System.StringComparison.Ordinal);
            bool   srcWildcard   = wildcardIndex >= 0;
            string needCheckPath = sourPath;

            if (srcWildcard)
            {
                int lastSlashIndex = sourPath.LastIndexOf('/');
                if (lastSlashIndex > wildcardIndex)
                {
                    throw new DoodleException(string.Format("sourPath {0} 是不合法的路径!", sourPath));
                }

                needCheckPath = Path.GetDirectoryName(sourPath);
            }

            var sourState = GetPathState(needCheckPath);

            if (sourState == PathState.None)
            {
                throw new DoodleException(string.Format("{0} 路径不存在!", needCheckPath));
            }

            var destState = GetPathState(destPath);

            if (!force && destState != PathState.None)
            {
                throw new DoodleException("destPath is not empty!");
            }

            if (srcWildcard)
            {
                if (destState == PathState.File)
                {
                    if (force)
                    {
                        PathUtil.RemovePath(destPath);
                    }
                    else
                    {
                        throw new DoodleException(string.Format("destPath {0} 不能是一个文件,除非指明force!", destPath));
                    }
                }

                var files = Directory.GetFiles(Path.GetDirectoryName(sourPath), Path.GetFileName(sourPath));
                DirUtil.TryCreateDir(destPath);
                foreach (var f in files)
                {
                    PathUtil.CopyPath(f, Path.Combine(destPath, Path.GetFileName(f)), force);
                }
            }
            else
            {
                var relation = GetPathRelation(sourPath, destPath);
                if (sourState == PathState.Dir && relation == PathRelation.Parent)
                {
                    throw new DoodleException("can't copy a directory into itself!");
                }

                if (destState == PathState.Dir && relation == PathRelation.Child)
                {
                    throw new DoodleException("can't overwrite sourPath's parent dir!");
                }

                DirUtil.TryCreateParentDir(destPath, force);
                TryRemovePath(destPath);
                if (sourState == PathState.File)
                {
                    File.Copy(sourPath, destPath);
                }
                else if (sourState == PathState.Dir)
                {
                    DirUtil.CopyDir(sourPath, destPath, true);
                }
            }
        }