如何使用getdirectories读取具有空间的文件夹名称 [英] How to read folder name having space using getdirectories

查看:163
本文介绍了如何使用getdirectories读取具有空间的文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我收到错误

Could not find a part of the path





当我使用时GetDirectories



我尝试过:





when i am using GetDirectories

What I have tried:

void DeleteFile(string sDir,string fileExt)
{
    Program pm = new Program();
    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())
                {
                    File.Move(f, f.Replace(" ", "_"));
                    CleanPath(f.Replace(" ", "_"));
                }
            }
            DeleteFile(d, fileExt);
        }
    }
    catch (System.Exception excpt)
    {
        pm.WriteErrorLogFile("Error :", excpt.Message);
    }

推荐答案

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

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.


这篇关于如何使用getdirectories读取具有空间的文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆