The program below creates a snapshot of itself by pressing the P key. It creates the file in the directory the program is in. For example the first time you press the P key it creates the file snapshot.png, if you press it again it creates snapshot_1, third time snapshot_2 and so on...
But if you close the program and start it again and those files are existing now in the directory, if now you press P key it overwrites them...
What I want is the program to check if the file that it tring to create exist, and if so, to try to create a file with the next number in the row.. and if that exist too, then try the next, and so on, untill it finds a file name that not exist and only then to create the file.
Can you guys help me modify the code to do what i'm describing?
Thanks
What I have tried:
string filename = "\\screenshot.png";
private void Mainwindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.P)
{
FrameworkElement element = UxVisual as FrameworkElement;
var pathstr =
System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
if (System.IO.File.Exists(pathstr + filename))
{
try
{
string i =
filename.Substring(filename.LastIndexOf('_') + 1,
filename.LastIndexOf('.') - (filename.LastIndexOf('_')
+ 1));
filename = "\\screenshot_" + (Convert.ToInt32(i) +
1).ToString() + ".png";
}
catch
{
filename = "\\screenshot_1.png";
}
}
Uri path = new Uri(pathstr + filename);
CaptureScreen(element, path);
}