2

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

4
  • Can you step through the code and let me know if the error is on Directory.GetFiles or on resultBitmap.Save? I am running similar code and not getting errors. Commented Jan 29, 2017 at 5:39
  • I'm sure the error is on Directory.GetFiles, i've trying this code and i got same error message: string filepath = "D:\\aaaa"; string[] dirs = Directory.GetFiles(filepath, "*.jpg"); foreach (string imageFile in dirs) { Invoke(new Action(delegate () { richTextBox1.AppendText(imageFile + Environment.NewLine); })); Commented Jan 29, 2017 at 5:52
  • Does this have to do anything with access rights? Could you try this in some other folder than the bin/debug/ and give read access to everyone? Commented Jan 29, 2017 at 8:44
  • Using Environment.CurrentDirectory is almost never correct. The exception message leaves no doubt, that directory does not exist. Crystal ball says that you only have a C:\Users\myPC\Proj\Image Blur\aaaa directory. Use a post-build event to xcopy a directory to bin\Debug. Commented Jan 29, 2017 at 9:03

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;?

2 Comments

thank you for replying, but the error message shown before "string fullpath" line,
You need to add a check if (Directory.Exists(filepath)) before running the method. The result is you're just able to reach that directory or it doesn't exist for some reason. Maybe it hasn't been created yet since it's in the debug folder? I'm not sure but Jesse Good is also right... In the code example above, even if you make it into the foreach loop you're combining 2 full paths. imageFile is the full path and file name and all you'll need.
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.