System.IO.DirectoryNotFoundException,找不到路径的一部分

时间:2017-01-29 05:23:36

标签: c#

我正在尝试查找特定目录中的所有jpg文件。 但是我收到了这个错误

  

其他信息:找不到路径'C:\ Users \ myPC \ Proj \ Image Blur \ bin \ Debug \ aaaa'的一部分。

private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
    {
        ApplyFilter(false);
        string filepath = Environment.CurrentDirectory + "\\aaaa\\";
        ImageFormat imgFormat = ImageFormat.Jpeg;
        foreach (var imageFile in Directory.GetFiles(filepath, "*.jpg"))
        {
            string fullPath = filepath + imageFile;
            try
            {
                ExtBitmap.BlurType blurType =
                ((ExtBitmap.BlurType)cmbBlurFilter.SelectedItem);

                resultBitmap.ImageBlurFilter(blurType);
                resultBitmap.Save(fullPath, imgFormat);
                resultBitmap = null;
            }
            catch
            {
            }
        }
    }

路径存在,并且还包含jpg文件 谢谢

4 个答案:

答案 0 :(得分:1)

请参阅Directory.GetFiles文档:

  

返回值类型:System.String []

     

。中文件的全名(包括路径)数组   指定的目录,与指定的搜索模式匹配,或者   如果没有找到文件,则为空数组。

因此,当您执行string fullPath = filepath + imageFile;时,您将两条完整路径连接在一起。

我不是100%确定您使用第string fullPath = filepath + imageFile;行做了什么?

答案 1 :(得分:0)

试试这个:

private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
    ApplyFilter(false);
    string filepath = Environment.CurrentDirectory + "\\aaaa\\";
    ImageFormat imgFormat = ImageFormat.Jpeg;
    foreach (var imageFile in Directory.GetFiles(filepath, "*.jpg"))
    {
        string imageName = Path.GetFileName(imageFile);//Add this
        string fullPath = filepath + imageName;//Update here
        try
        {
            ExtBitmap.BlurType blurType =
            ((ExtBitmap.BlurType)cmbBlurFilter.SelectedItem);

            resultBitmap.ImageBlurFilter(blurType);
            resultBitmap.Save(fullPath, imgFormat);
            resultBitmap = null;
        }
        catch
        {
        }
    }
}

答案 2 :(得分:0)

我有这个例外,我可以看到该文件位于该文件夹中。 原来这是因为该文件位于已安装的驱动器上,该驱动器是为我登录的用户安装的,但未为运行该应用程序的用户安装。

为应用程序用户安装驱动器进行修复。

答案 3 :(得分:0)

尝试使用AppDomain.CurrentDomain.BaseDirectory代替Environment.CurrentDirectory

Environment.Current目录的值可以在运行应用程序的过程中更改。