コード例 #1
0
        public static string MoveFile(String sourceFile, String outputRootFolder, String outputFilename = "", String sourceRootFolder = "")
        {
            // TODO:  Check existance of sourceFile, sourceRootFolder

            String   relativeFolder = "";
            FileInfo fiSource       = new FileInfo(sourceFile);

            if (outputFilename == "")
            {
                outputFilename = new FileInfo(sourceFile).Name;
            }

            if (sourceRootFolder != "")
            {
                relativeFolder = getRelativeFolder(sourceRootFolder, fiSource.DirectoryName);
            }

            DirectoryInfo diOutputRootFolder = new DirectoryInfo(outputRootFolder);

            if (!diOutputRootFolder.Exists)
            {
                diOutputRootFolder.Create();
            }

            DirectoryInfo diOutputFolder = new DirectoryInfo(Path.Combine(diOutputRootFolder.FullName, relativeFolder));

            if (!diOutputFolder.Exists)
            {
                diOutputFolder.Create();
            }

            FileInfo fiOutputFile    = new FileInfo(outputFilename);
            String   outFullFileName = String.Format(@"{0}\{1}{2}",
                                                     diOutputFolder.FullName, fiOutputFile.BaseFilename(), fiOutputFile.Extension);

            for (int i = 0; File.Exists(outFullFileName); i++)
            {
                outFullFileName = String.Format(@"{0}\{1}_{2}{3}",
                                                diOutputFolder.FullName, fiOutputFile.BaseFilename(), i, fiOutputFile.Extension);
            }
            fiSource.MoveTo(outFullFileName);
            return(outFullFileName);
        }