获取包含.jpg文件C#.net的所有文件夹名称 [英] get all the folders name containing .jpg files C#.net

查看:69
本文介绍了获取包含.jpg文件C#.net的所有文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中

In my code

string temp = @"E:\";
string[] files = Directory.GetFiles(temp, "*.jpg", SearchOption.AllDirectories);
foreach (string file in files)
{
  filename = System.IO.Path.GetFileName(file);
  destFile = System.IO.Path.Combine(targetPath, filename);
  System.IO.File.Copy(file, destFile, true);
}



在运行时,我没有得到任何文件,并且foreach循环没有执行。



是否还有其他方法可以获取包含.jpg图像的E盘中的所有文件夹。


while running time ,i am not getting any files and foreach loop is not executing.

is there any other method for get all the folders in E drive that contains .jpg images.

推荐答案

您可以执行以下操作



You can do as the following

var files = Directory.EnumerateFiles("C:\\path", "*.*", SearchOption.AllDirectories)
            .Where(s => s.EndsWith(".mp3") || s.EndsWith(".jpg"));





请在此处阅读Christoffer的答案 http://stackoverflow.com/questions/163162/can-you-call-directory-getfiles-with-multiple-filters [ ^ ]



它可能对你有帮助。



Please read Christoffer's answer here http://stackoverflow.com/questions/163162/can-you-call-directory-getfiles-with-multiple-filters[^]

It may help you .


是否有电子驱动器?它有jpgs吗?您是否已进入调试器?
Is there an e drive ? Are there jpgs in it ? Have you stepped through in the debugger ?


using System.Security.Principal;
using System.IO;
 public ActionResult Index()
        {
            WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
            WindowsPrincipal currentPrincipal = new WindowsPrincipal(currentIdentity);
            if (currentPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                string temp = @"D:\", filename, destFile;
                string[] files = Directory.GetFiles(temp, "*.jpg", SearchOption.TopDirectoryOnly);
                foreach (string file in files)
                {
                    filename = System.IO.Path.GetFileName(file);
                    destFile = System.IO.Path.Combine(@"D:\Images", filename);
                    System.IO.File.Copy(file, destFile, true);
                }
            }
            return View("Index");
        }


这篇关于获取包含.jpg文件C#.net的所有文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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