0

这个程序应该显示一个目录的路径和目录如果它存在那么它还应该显示带有以下扩展名的文件(即.doc、.pdf、.jpg、.jpeg)我创建了一个名为 max 的文件夹代码正在运行的 bin 目录,但它告诉我该目录不存在这是我下面的代码

namespace sharp_lab_2
{
    interface IFileOperation
    {
        //should  handle the file
        bool Accept(string fileName);
        //handler function file
        void Process(string fileName);
    }

    class FileProcByExt : IFileOperation
    {
        string extName;
        string folderName;
        public FileProcByExt(string ext = "")
        {
            extName = ext;
            if (extName == "")
                folderName = "MAXee";
            else
                folderName = extName.ToUpper();
        }
        public bool Accept(string fileName)
        {
            bool res = false;
            if (extName == "" ) res = true; // all
            if (Path.GetExtension(fileName) == "." + extName) res = true; // has extension
            return res;
        }
        public void Process(string fileName)
        {
            Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(fileName),
                                                   folderName));
            File.Move(fileName,
                      Path.Combine(Path.GetDirectoryName(fileName),
                                   folderName,
                                   Path.GetFileName(fileName)));            
        }
    }
    class FileProcNameAfter10 : IFileOperation
    {
        static int cnt;
        public bool Accept(string fileName)
        {
            return Path.GetFileNameWithoutExtension(fileName).Length > 10;
        }
        public void Process(string fileName)
        {
            File.Copy(fileName,
                      Path.Combine(Path.GetDirectoryName(fileName),
                                   "longname_" + cnt + Path.GetExtension(fileName)));
            cnt++;
        }
    }
    class FileProcEnc: IFileOperation
    {
        public bool Accept(string fileName)
        {
            FileInfo fi = new FileInfo(fileName);
            return fi.Length < 1024;
        }
        public void Process(string fileName)
        {
            File.Encrypt(fileName);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {             
                string directoryPath = args[0];
                string[] filesList, filesListTmp;
                IFileOperation[] opList = { new FileProcNameAfter10(),
                                            new FileProcEnc(),
                                            new FileProcByExt("jpeg"),
                                            new FileProcByExt("jpg"),
                                            new FileProcByExt("doc"),
                                            new FileProcByExt("pdf"),
                                            new FileProcByExt("djvu")
                                            };
                if (Directory.Exists(directoryPath))
                {
                    filesList = Directory.GetFiles(directoryPath);
                    while (true)
                    {
                        Thread.Sleep(500);
                        filesListTmp = Directory.GetFiles(directoryPath);
                        foreach (var elem in Enumerable.Except<string>(filesListTmp, filesList))
                        {
                            Console.WriteLine(elem);
                            foreach (var op in opList)
                            {
                                if (op.Accept(elem)) op.Process(elem);
                            }    
                        }
                        filesList = filesListTmp;
                        if (Console.KeyAvailable == true && Console.ReadKey(true).Key == ConsoleKey.Escape) break;
                    }
                }
                else
                {
                    Console.WriteLine("There is no such directory.");
                    Console.ReadKey();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error." + ex.Message);

            }
        }
    }
}
4

1 回答 1

0

你应该让你的代码获得所需的权限。如果您没有对该目录的最低只读权限,则 Exists 方法将返回 false。如果在尝试确定指定文件是否存在时发生任何错误,Exists 方法将返回 false。这可能发生在引发异常的情况下,例如传递包含无效字符或过多字符的文件名、磁盘故障或丢失,或者调用者没有读取文件的权限。

微软

于 2013-03-03T08:51:25.023 回答