2016-11-19 93 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