Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
        }
Posted
Updated 25-Apr-19 0:44am
v2

1 solution

Use Directory.GetFiles Method (System.IO) | Microsoft Docs[^] to get the all the files in the pattern "snapshot*", and find the last one in the sorted result. You can then determine which number to use for the first one.
 
Share this answer
 
Comments
tool__ 25-Apr-19 7:36am    
I can't figure out how to use this method with the existing code, can you modify the code above for me?
Richard MacCutchan 25-Apr-19 8:43am    
Sorry, no. At the start of the application you need to get the list of existing files and find the one with the highest number suffix. You can then start saving from that point on. As an alternative you could save and restore a value in the user config settings. See How To: Write User Settings at Run Time with C# | Microsoft Docs[^] for details.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900