文件.移动/文件.复制什么也没发生

问题描述 投票:0回答:1

我正在尝试编写代码以将某些文件移动(或复制)到某些文件夹中(实际上是按扩展名对这些文件进行分类。)

代码中的所有内容都工作正常(扫描文件夹并发现文件,提取其扩展名并创建文件夹并用扩展名命名它们),除非它应该将文件移动/复制到相应的文件夹中。它对这个任务没有任何作用。

这是代码(如果您需要文件和文件夹来测试我的代码,问题末尾会提供包含一些示例文件和文件夹的压缩文件的链接。)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ramezanian_VCS_SysProg_Course
{
    class Program
    {
        static void Main()
        {
            /* string variables to hold working directory path and 
                an array of filenames and a list of extensions. */
            String ThisPath = @"E:\HDR1";
            String[] ThisFiles = null;
            List<string> NotDottedExtensionContainer = new List<string>();

            //Getting filevnames with their full path.
            ThisFiles = Directory.GetFiles(ThisPath);

            /* Creating a text log file and also a list of file extensions while trimming and capitalizing them
              like this: file.txt -> .txt -> TXT */
            try
            {
                using (StreamWriter ThisLogFile = new StreamWriter(Path.Combine(ThisPath, "Log_File.txt")))
                //
                foreach (String _F in ThisFiles)
                    {
                        NotDottedExtensionContainer.Add(Path.GetExtension(_F).TrimStart('.').ToUpper());
                        ThisLogFile.WriteLine(_F);
                    }

            }
            catch (Exception)
            {

                throw; //In case something oes wrong.
            }

            UInt16 Temp_CountOfExtensions = 0; //A temporary variable to count how many extensions are there and so
            // how many folders will be created then.

            ThisPath = @"E:\HDR1\";
            //Making a distinct collection of unique extensions...
            IEnumerable<string> UniqueExtensionsCollection = NotDottedExtensionContainer.Distinct();
            foreach (String TempItem1 in UniqueExtensionsCollection)
            {
                Console.WriteLine(TempItem1);
                //Then creating based on that distinct list.
                Directory.CreateDirectory(ThisPath + TempItem1);
                Temp_CountOfExtensions++;
            }
            foreach (String TempItem2 in UniqueExtensionsCollection)
            {
                foreach (String TempItem3 in ThisFiles)
                {
                    //This compares each file extension with subfolder names...
                    if (Path.GetExtension(TempItem3).TrimStart('.').ToUpper() == TempItem2)
                    {
                        //... To move them into their right place...
                        File.Move(TempItem3, ThisPath + TempItem2 + Path.GetFileName(TempItem3));
                    }
                    //...but nothing happens!
                }
            }
            Console.WriteLine("Total of {0} subfolders were created in {1}.",Temp_CountOfExtensions, ThisPath);
            Console.ReadLine();
        }
    }
}

这是输出(所有文件夹均已使用所需名称成功创建。):

链接到输出屏幕截图

用于测试代码的示例工作目录:点击此处

c# file-copying file-move
1个回答
2
投票

扩展文件夹和文件之间需要有斜杠

File.Move(TempItem3, ThisPath + TempItem2 + "\\" + Path.GetFileName(TempItem3));

只要第二条路径不以反斜杠开头,您也可以使用 Path.Combine 来完成此操作,

File.Move(TempItem3, Path.Combine(ThisPath, TempItem2, Path.GetFileName(TempItem3)));
© www.soinside.com 2019 - 2024. All rights reserved.