Automatically increment file name

Right now this is my code

int number = 0;
DirectoryInfo di = new DirectoryInfo(scpath + @"Screenshots\");

if (di.Exists) {

} else {
    di.Create();
}
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
bmpScreenShot.Save(di + "Screenshot_" + number, ImageFormat.Jpeg);

The program takes a screenshot (which works) and then saves the snapshot. What I want to do is check the program and see if the screenshot “Screenshot_ *” exists, and if you do not create it. If so, add until it reaches a number that was not used at the end of Screenshot_ Not sure how to do this, given that it is larger with files and increasing. I think the for loop, but now I play with it.

+5
source share
6 answers

Like @Quintin, use datetime for the file name:

string filename = Path.Combine(
    di.FullName,
    String.Format("{0}.jpg", DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss")));
bmpScreenShot.Save(filename, ImageFormat.Jpeg);
+6
source

Getting the name of a file that does not exist sounds like a job for a method.

string IndexedFilename(string stub, string extension) 
{
    int ix = 0;
    string filename = null;
    do {
        ix++;
        filename = String.Format("{0}{1}.{2}", stub, ix, extension);
    } while (File.Exists(filename));
    return filename;
}

, . , , , .

:

string di = Path.Combine(scpath, "Screenshots");
if (!Directory.Exists(di) { 
    Directory.Create(di); 
} 
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width; 
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height; 
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight); 
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot); 
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
string filename = IndexedFilename(Path.Combine(di,"Shot_"),"jpg");
bmpScreenShot.Save(filename, ImageFormat.Jpeg); 
+8

Instead of using a number as a way to distinguish between screenshots, use a timestamp:

string currentDT = string.Format("{0:D4}.{1:D2}.{2:D2}-{3:D2}.{4:D2}.{5:D2}",
                   DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day,
                   DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second)
bmpScreenShot.Save(di + "Screenshot_" + currentDT, ImageFormat.Jpeg); 
+2
source

This is an opportunity

string[] files = System.IO.Directory.GetFiles(scpath, "Screenshot_*.jpg");
string filename;
int i = 0;
do {
    filename = "Screenshot_" + ++i + ".jpg";
} while (files.Contains(filename));
+2
source

I would use a GUID ...

try{
    bmpScreenShot.Save(di + "Screenshot_" + Guid.NewGuid().ToString(), ImageFormat.Jpeg);
}catch(Exception e)
{ 
    //handle the problems here, for example if file already exists, try again
}

This should work well until you complete the unique GUIDs ...

+2
source
public static string MakeUniqueFileName(string file)
{
    string dir = Path.GetDirectoryName(file);
    string fn;

    for (int i = 0; ; ++i)
    {
        fn = Path.Combine(dir, string.Format(file, i));

        if (!File.Exists(fn))
            return fn;
    }
}

Use it as follows:

string file = scpath + @"Screenshots\" + "Screenshot_{0}.png";
bmpScreenShot.Save(MakeUniqueFileName(file), ImageFormat.Jpeg);
+1
source

All Articles