Esempio n. 1
0
        public static void MoveFile(string path, string destPath, FileCopyMode copyMode = FileCopyMode.SaveExisting)
        {
            if (!File.Exists(destPath))
            {
                File.Move(path, destPath);
                return;
            }

            if (copyMode == FileCopyMode.ReplaceExisting)
            {
                File.Delete(destPath);
                File.Move(path, destPath);
                return;
            }

            if (copyMode == FileCopyMode.SaveExisting)
            {
                var i     = 0;
                var ext   = Path.GetExtension(destPath);
                var index = destPath.Length - ext.Length;
                while (true)
                {
                    ++i;
                    var nPath = destPath.Insert(index, "_" + i);
                    if (!File.Exists(nPath))
                    {
                        File.Move(path, nPath);
                        return;
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// フォルダをコピーします
        /// 指定したフォルダ配下のフォルダとファイルを、指定したフォルダ配下へ全てコピーします
        /// </summary>
        /// <param name="fromDirPath">コピー元フォルダのパス</param>
        /// <param name="toDirPath">コピー先フォルダのパス</param>
        /// <param name="copyMode">コピーモード</param>
        public static void CopyDir(string fromDirPath, string toDirPath, FileCopyMode copyMode)
        {
            //コピー先がコピー元のフォルダ配下にある場合は、無限ループとなるためエラーとします(実際には無限ループになる前に、パスの文字列長のエラーが発生する)
            if (ContainsDir(fromDirPath, toDirPath))
            {
                throw new IOException("CopyDir() コピー先がコピー元に含まれています");
            }

            //フォルダを丸ごとコピーします
            CopyDirSub(fromDirPath, toDirPath, copyMode);
        }
Esempio n. 3
0
        /// <summary>
        /// ファイルコピー可能かどうかを返します
        /// </summary>
        /// <param name="fromPath">コピー元のパス</param>
        /// <param name="toPath"  >コピー先のパス</param>
        /// <param name="copyMode">コピーモード</param>
        /// <returns></returns>
        private static bool CanFileCopy(string fromPath, string toPath, FileCopyMode copyMode)
        {
            switch (copyMode)
            {
            case FileCopyMode.CopyAlways:
                return(true);    //コピー可

            case FileCopyMode.DontCopy:
                //コピー先に同じファイルがあるか?
                if (File.Exists(toPath))
                {
                    //ある場合
                    return(false);    //コピー不可
                }
                else
                {
                    //ない場合
                    return(true);     //コピー可
                }

            case FileCopyMode.CopyIfNewer:
                //コピー先に同じファイルがない場合はコピー可
                if (File.Exists(toPath) == false)
                {
                    return(true);                                  //コピー可
                }
                //更新日付を比較して、コピー元の方が新しい場合はコピー可とします
                var fromDateTime = File.GetLastWriteTime(fromPath);
                var toDateTime   = File.GetLastWriteTime(toPath);
                if (fromDateTime > toDateTime)
                {
                    //コピー元が新しい場合
                    return(true);     //コピー可
                }
                else
                {
                    return(false);    //コピー不可
                }

            default:
                //コーディングミス:コピーモードを増やして、case の追加漏れがあった場合に来ます
                throw new Exception();
            }
        }
Esempio n. 4
0
        private static void CopyDirSub(string fromDirPath, string toDirPath, FileCopyMode copyMode)
        {
            //コピー元のフォルダが存在しない場合は処理しない
            if (Directory.Exists(fromDirPath) == false)
            {
                return;
            }

            //フォルダ配下のファイルを全てコピーします
            foreach (var fromPath in Directory.GetFiles(fromDirPath))
            {
                //コピー先のパス
                var toPath = AppendPath(toDirPath, Path.GetFileName(fromPath));

                //コピーモードに応じてファイルコピー可能かどうかをチェックします
                if (CanFileCopy(fromPath, toPath, copyMode))
                {
                    //ファイルコピー可能な場合、ファイルコピーします
                    File.Copy(fromPath, toPath, true);
                }
            }

            //フォルダ配下のファイルを全てコピーします
            foreach (var dir in Directory.GetDirectories(fromDirPath))
            {
                //フォルダ配下のフォルダのパスを作成します
                var toChildDirPath = AppendPath(toDirPath, Path.GetFileName(dir));

                //フォルダがない場合は、フォルダを作成します
                if (Directory.Exists(toChildDirPath) == false)
                {
                    Directory.CreateDirectory(toChildDirPath);
                }

                //フォルダ配下のフォルダとファイルを再起呼び出しでコピーします
                CopyDirSub(dir, toChildDirPath, copyMode);
            }
        }
        static void SetCopyToOutputDirectory(this ProjectFile item, object value)
        {
            FileCopyMode copyToOutputDirectory = ConvertToCopyToOutputDirectory(value);

            item.CopyToOutputDirectory = copyToOutputDirectory;
        }