C#:获取文件夹下的所有bmp图像

demo

static void Main()
{
   string path = @"C:\Users\Administrator\Desktop\A";
   string[] files = Directory.GetFiles(path, "*.bmp");
   int cnt = 0;
   foreach(string file in files)
   {
     cnt++;
     xxx rawImg =  xxx.FromFile(file);//读取图像
     ... ...
     

      Console.WriteLine(file);
   }
}

上述程序的作用是读取A路径下格式为.bmp的图像;

命令

1. 获得当前运行程序的路径

string rootPath = Directory.GetCurrentDirectory();

2. 获得该文件夹下的文件,返回类型为FileInfo
string path=@"X:\XXX\XX"; //文件夹路径
DirectoryInfo root = new DirectoryInfo(path);
FileInfo[] files=root.GetFiles();

3. 获得该文件夹下的子目录,返回类型为DirectoryInfo
string path=@"X:\XXX\XX";
DirectoryInfo root = new DirectoryInfo(path);
DirctoryInfo[] dics=root.GetDirectories();

4. 获得路径下的文件夹名字
string path=@"X:\XXX\XX";
DirectoryInfo root = new DirectoryInfo(path);
string dicName=root.Name;

5. 获得文件夹完整的路径名
string path=@"X:\XXX\XX";
DirectoryInfo root = new DirectoryInfo(path);
string dicName=root.FullName;

6. 获取文件的Name和FullName
string path=@"X:\XXX\XX";
DirectoryInfo root = new DirectoryInfo(path);
foreach (FileInfo f in root.GetFiles())
{
     string name=f.Name;
     string fullName=f.FullName;
}    

7. 只获取目录下一级的文件夹与文件
String path = @"X:\xxx\xxx";
//方法一:获取目录下txt文件
string[] files = Directory.GetFiles(path, "*.txt");      
foreach (string file in files)
{
    Console.WriteLine(file);
}
 
//方法二:获取目录下txt文件
DirectoryInfo folder = new DirectoryInfo(path);        
foreach (FileInfo file in folder.GetFiles("*.txt"))
{
    Console.WriteLine(file.FullName);
}

参考文章

1. https://www.cnblogs.com/willingtolove/p/9235353.html

 

 

 

 

 

 

你可能感兴趣的:(C#)