c# - 第二个路径片段不能是驱动器或 UNC 名称

标签 c# bitmap path environment-variables image-file

如何通过主文件夹中的环境路径文件夹读取文件夹中的所有图像文件:

   string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "mainFolder"); 

例如,如果一个文件路径的内部文件夹是:

  Bitmap bmp = new Bitmap(path + "/folder/pic1.bmp");

但我想读取 mainFolder/folder 的所有文件:

pic1.bmp
pic2.bmp
pic3.bmp
pic5.bmp

不确定如何正确执行:

foreach (string imageFileName in Directory.GetFiles(path, "/folder/*.bmp"))
{
    using (Bitmap bmp = new Bitmap(imageFileName))
    {
      // process 
    }
}

这样我有:第二个路径片段不能是驱动器或 UNC 名称

这样:

foreach (string imgFileName in Directory.GetFiles(path + "/folder/*.bmp"))

我得到:路径中有非法字符。

最佳答案

此代码失败,因为您必须提供文件名的模式作为第二个参数:

foreach (string imgFileName in Directory.GetFiles(path, "/folder/*.jpg"))

第二个失败,因为“*”是一个特殊符号,UNC 路径不接受它。

foreach (string imgFileName in Directory.GetFiles(path + "/folder/*.jpg"))

所以你可以尝试做以下事情:

foreach (string imgFileName in Directory.GetFiles(path + "/folder/","*.jpg")) 

MSDN Directory.GetFiles

关于c# - 第二个路径片段不能是驱动器或 UNC 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44987281/

相关文章:

android - 如何在 android 中将 Bitmap 转换为 Drawable?

c# - 使用 C# 获取以毫秒为单位的时间

c# - A*探路者障碍物碰撞问题

c# - 从半透明位图创建 GraphicsPath

android - 在 imageView 中显示最后拍摄的照片? (相机)

javascript - 在 Node 应用程序中定义路径树

java - 将字体文件作为 File 对象获取或获取其路径

正则表达式 : Strip path until last occurence in a capturing group

c# - C#中如何将数组的内容打印到标签中

c# - 两个 ulong(unsigned long)值之差的 Math.Abs