2016-11-19 111 views
0

每次上傳者裁剪圖像時我都會保存一個新圖像,並且我想檢查每個新圖像是否存在。我有這樣的代碼:檢查目錄中是否存在多個文件

private void pictureBox5_MouseUp(object sender, MouseEventArgs e) 
     { 
      Selecting = false; 

      // Copy the selected area. 
      SelectedArea = GetSelectedArea(pictureBox5.Image, Color.Transparent, Points); 

      SelectedArea.Save(@"C:\Users\User\Desktop\Gallery\image1cropped.png", ImageFormat.Png); 


     } 

我希望它像保存image2cropped,image3cropped ..並檢查它是否存在像

if(File.Exists(@"C:\Users\User\Desktop\Gallery\image1cropped.png", ImageFormat.Png); 

我想它來檢查像image2cropped,image3cropped ..等等。 想法?

回答

0

如果我明白了:你正在保存一個區域到一個文件。第一次,你將它保存到image1cropped.png,然後你想automaticaly使用image2cropped爲了不覆蓋第一個文件,對不對? 在這種情況下,您可以使用此代碼:

 int i = 0; // set 0 to start at 1 for "image1cropped" 
     string freeFileName; // the final fileName that doesn't exist 

     // loop while file imageXcropped exists 
     do 
     { 
      i++; 
      freeFileName = @"C:\Users\User\Desktop\Gallery\image" + i + "cropped.png"; 
     } while (File.Exists(freeFileName)); 

     // at this point freeFileName doesn't exists, you can use it 
     // use : SelectedArea.Save(freeFileName, ImageFormat.Png); 
+0

是的解救它的東西。但我想檢查它們是否存在是相同的算法? –

+1

沒關係我解決了它。 (@「C:\ Users \ User \ Desktop \ Gallery \ image」+ NumberOfClick.ToString()+「cropped.png」,ImageFormat.Png);這是我的最終代碼: SelectedArea.Save string filename = @「C:\ Users \ User \ Desktop \ Gallery \ image」+ NumberOfClick.ToString()+「cropped.png」; if(File.Exists(filename)) {button1.Visible = true; pictureBox5.Visible = false; } –

+0

是否要在保存之前檢查它是否覆蓋它或保存後檢查它是否保存完好? – Elloco

相關問題