userrrrrrr
userrrrrrr

Reputation: 65

System.IO.DirectoryNotFoundException, Could not find a part of the path

I'm trying to find all jpg files in a specific directory, but i'm getting this error

Additional information: Could not find a part of the path '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
            {
            }
        }
    }

The path does exist, and also contains jpg files

Upvotes: 2

Views: 67695

Answers (4)

umair qayyum
umair qayyum

Reputation: 286

Try using AppDomain.CurrentDomain.BaseDirectory instead of Environment.CurrentDirectory

The Environment.Current Directory has a value that can change through the course of running your application.

Upvotes: 0

Taran
Taran

Reputation: 14176

I had this exception, and I could see the file sitting in the folder. Turned out it was because the file was on a mounted drive, which was mounted for the user I was logged in as, but not mounted for the user the application was running under.

Mounting the drive for the application user fixed it.

Upvotes: 2

Ali Ezzat Odeh
Ali Ezzat Odeh

Reputation: 2163

Try this :

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

Upvotes: 0

Jesse Good
Jesse Good

Reputation: 52365

Please see the Directory.GetFiles documentation:

Return Value Type: System.String[]

An array of the full names (including paths) for the files in the specified directory that match the specified search pattern, or an empty array if no files are found.

So, when you do string fullPath = filepath + imageFile; you are concatenating two full paths together.

I'm not 100% sure what you are trying do with the line string fullPath = filepath + imageFile;?

Upvotes: 1

Related Questions