且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用getdirectories读取具有空间的文件夹名称

更新时间:2023-02-14 20:57:20

我首先使用调试器并查看您正在传递的内容和这样做。当我粘贴你的代码并尝试它时:

I'd start by using the debugger and looking at exactly what you are passing and doing. When I paste your code in and try it:
string sDir = @"A:\ZDCIM\A Temporary Folder\";
string fileExt = ".txt";
try
    {
    foreach (string d in Directory.GetDirectories(sDir))
        {
        foreach (string f in Directory.GetFiles(d))
            {
            var extension = Path.GetExtension(f).ToUpper();
            if (extension == fileExt.ToUpper())
                {
                Console.WriteLine(f);
                }
            }
        //DeleteFile(d, fileExt);
        }
    }
catch (System.Exception excpt)
    {
    Console.WriteLine("Error :", excpt.Message);
    }



它工作正常,我得到临时文件夹的所有子文件夹中的所有文本文件,无论空格如何。



所以我怀疑它是sDir或fileExt是错误的 - 可能sDir不是完整路径或相对于错误的基本文件夹。


It works fine, and I get all the text files in all the subfolders of the temporary folder, regardless of spaces.

So I'd suspect that it's the sDir or fileExt that are wrong - probably sDir isn't a full path or is relative to the wrong base folder.