将特定的文件扩展名从目录移动到目录

时间:2018-10-11 19:33:09

标签: c#

我正在尝试执行此语法以将文件从我的scandir移到我的movedir上,代码可以很好地编译,并且在运行时没有出现错误-但是文件没有移动?

我正在通过monodev在UNIX计算机上运行它。

class MainClass
{
    private const string scandir = "/home/owner/Downloads/";
    private const string movedir = "/home/owner/Documents/Videos/";

    private static void Main(string[] args)
    {
        MoveFiles();
    }

    private static void MoveFiles()
    {
        var MyFiles = Directory.EnumerateFiles(scandir, "*.*", SearchOption.AllDirectories)
            .Where(f => MeetsCriteria(f))
            .GroupBy(f => Path.GetFileName(f).ToLower())
            .Select(f => f.First());

        foreach (string file in MyFiles)
        {
            FileInfo mFile = new FileInfo(file);
            mFile.MoveTo(movedir + "\\" + mFile.Name);
        }
    }
    private static bool MeetsCriteria(string Filename)
    {
        var ext = Path.GetExtension(Filename).ToLower();

        return (ext == "avi" || ext == "mp4" || ext == "m4v" || ext == "mkv");
    }
}

1 个答案:

答案 0 :(得分:0)

Path.GetExtension()返回包含前导点的文件扩展名。因此,您的MeetsCriteria方法永远不会匹配扩展名。

在文件扩展名中添加前导点:

return (ext == ".avi" || ext == ".mp4" || ext == ".m4v" || ext == ".mkv");