c#遍历文件夹和文件

来源:互联网 发布:微博来自mac客户端 编辑:程序博客网 时间:2024/05/01 15:52

1、获取指定文件夹的全路径

适用于不知道文件夹的准确名称,使用关键词获取文件夹的全称,并返回全路径

private string GetFolderName(string path,string key)        {            DirectoryInfo theFolder = new DirectoryInfo(path);            //遍历文件夹            foreach (DirectoryInfo NextFolder in theFolder.GetDirectories())            {                if (NextFolder.Name.ToUpper().IndexOf(key.ToUpper()) >= 0)                {                    return NextFolder.Name;                }            }            return " ";        }



2、获取指定文件夹下所有的pdf文件全路径

private List<string> GetAllPDFFiles()        {            List<string> fileNames = new List<string>();            string mainPath = @"Y:\Tech-center\test\";            string customerKey = "POXXXXX";            string customerFolder = GetFolderName(mainPath, customerKey);            mainPath += customerFolder;            DirectoryInfo theFolder = new DirectoryInfo(mainPath);            //遍历文件            foreach (FileInfo NextFile in theFolder.GetFiles())            {                if (NextFile.Extension.ToUpper() == ".PDF")                {                    fileNames.Add(NextFile.FullName);                }            }            return fileNames;        }



0 0
原创粉丝点击