2017-01-29 36 views
2

特定のディレクトリにあるすべてのjpgファイルを検索しようとしています。 しかし、私は、このエラーにSystem.IO.DirectoryNotFoundException、パスの一部を見つけることができませんでした

追加情報を取得しています:パスの一部が見つかりませんでした「Cの:\ユーザーがMYPC \メニュープロジェクト\イメージブラー\ binに\デバッグ\の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 
      { 
      } 
     } 
    } 

パスが存在している、とも はあなたに

+0

は、あなたがコードをステップ実行し、エラーがDirectory.GetFiles上またはresultBitmap.Save上にあるなら、私が知っていることはできますか?私は似たようなコードを実行していますが、エラーは出ません。 –

+0

私はこのエラーがDirectory.GetFilesにあると確信しています。私はこのコードを試していますが、同じエラーメッセージが出ています:string filepath = "D:\\ aaaa"; string [] dirs = Directory.GetFiles(filepath、 "* .jpg"); foreach(文字列内のイメージファイル) { Invoke(新しいアクション(delegate(){richTextBox1.AppendText(imageFile + Environment.NewLine);}))); – userrrrrrr

+0

これはアクセス権を持つ何かをする必要がありますか?あなたはbin/debug /よりも他のいくつかのフォルダでこれを試してみて、みんなに読み取りアクセス権を与えることができますか? – Developer

答えて

1

ありがとうJPGファイルが含まれていDirectory.GetFilesのドキュメントを参照してください:

戻り値型:[]

可能System.Stringをファイルの完全な名前(のパスを含むの配列)指定された検索パターンに一致する 指定のディレクトリ、またはファイルが見つからない場合は の空の配列。

したがって、string fullPath = filepath + imageFile;を実行すると、完全なパスが2つ連結されます。

string fullPath = filepath + imageFile;さんと何をしようとしているのですか?

+0

返信ありがとうございますが、 "文字列フルパス"行の前に表示されるエラーメッセージ – userrrrrrr

+0

メソッドを実行する前に(Directory.Exists(filepath))チェックを追加する必要があります。その結果、あなたはそのディレクトリにアクセスすることができますか、何らかの理由で存在しません。デバッグフォルダーに入ってからまだ作成されていない可能性がありますか?私は分かりませんが、Jesse Goodも正しいです...上のコード例では、foreachループに入れても2つのフルパスを組み合わせています。 imageFileは完全なパスとファイル名、そして必要なものだけです。 –

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 
     { 
     } 
    } 
} 
関連する問題