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

C# System.IO.DirectoryNotFoundException,找不到路径的一部分,c#,C#,我试图在一个特定的目录中找到所有jpg文件,但我得到了这个错误 其他信息:找不到路径“C:\Users\myPC\Proj\Image Blur\bin\Debug\aaaa”的一部分 路径确实存在,并且还包含jpg文件请参阅文档: 返回值类型:System.String[] 中文件的全名数组(包括路径) 与指定搜索模式匹配的指定目录,或 如果找不到文件,则为空数组 因此,当您执行string fullPath=filepath+imageFile将两个完整路径连接在一起 我不能100%确定您正

我试图在一个特定的目录中找到所有jpg文件,但我得到了这个错误

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

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

请参阅文档:

返回值类型:System.String[]

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

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

我不能100%确定您正在尝试对行
string fullPath=filepath+imageFile做什么

试试这个:

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
        {
        }
    }
}

我有这个例外,我可以看到文件夹中的文件。 事实证明,这是因为该文件位于已装入的驱动器上,该驱动器是为我登录的用户装入的,而不是为运行应用程序的用户装入的


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

尝试使用
AppDomain.CurrentDomain.BaseDirectory
而不是
Environment.CurrentDirectory


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

您是否可以逐步检查代码并告诉我错误是在Directory.GetFiles上还是在resultBitmap.Save上?我正在运行类似的代码,但没有收到错误。我确定错误在Directory.GetFiles上,我尝试了此代码,并收到了相同的错误消息:string filepath=“D:\\aaaa”;字符串[]dirs=Directory.GetFiles(filepath,*.jpg”);foreach(dirs中的字符串imageFile){Invoke(新操作(委托(){richTextBox1.AppendText(imageFile+Environment.NewLine);}));这与访问权限有关吗?您可以在bin/debug/以外的其他文件夹中尝试此操作,并向所有人授予读取权限吗?使用Environment.CurrentDirectory几乎从来都不正确。异常消息毫无疑问,该目录不存在。Crystal ball说您只有一个C:\Users\myPC\Proj\Image Blur\aaaa目录。使用生成后事件将目录复制到bin\Debug。感谢您的回复,但是如果在“string fullpath”行之前显示错误消息,您需要添加一个检查(directory.Exists(filepath))在运行该方法之前。结果是您刚好能够访问该目录,或者由于某种原因它不存在。可能它还没有被创建,因为它在调试文件夹中?我不确定,但Jesse Good也是正确的…在上面的代码示例中,即使您将它放入foreach循环,您也要合并2个完整路径。imageFile是e完整路径和文件名以及您需要的所有内容。
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
        {
        }
    }
}