jeudi 24 septembre 2015

Get latest "x.png" from a folder

I have a "take a screenshot" feature in my program. I am taking the screenshot and saving it with this code:

Bmp.Save(dir & "\MyApp\Screenshot" & random & ".png")

dir is Documents. and the random is this

random = CInt(Math.Ceiling(Rnd() * 99999)) + 1

But I don't want to save like this. I want to save in order. I want to check the folder, get the latest name and add +1 to it. Like If the latest one is Screenshot-17.png, save it as Screenshot-18.png.

I am thinking a substring but I don't know how can I get the latest png file from a folder.

Full code is here:

    Dim dir As String
    dir = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    Dim random As Integer
    random = CInt(Math.Ceiling(Rnd() * 99999)) + 1
    Dim pathh As String
    pathh = dir & "\MyApp\"
    If (Not System.IO.Directory.Exists(pathh)) Then
        System.IO.Directory.CreateDirectory(pathh)
    End If


    Dim Bmp As New Bitmap(WebControl1.ClientRectangle.Width, WebControl1.ClientRectangle.Height)
    Dim gBmp As Graphics = Graphics.FromImage(Bmp)
    gBmp.CopyFromScreen(WebControl1.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(WebControl1.Width, WebControl1.Height))
    Bmp.Save(dir & "\MyApp\Screenshot" & random & ".png")


    NotifyIcon1.ShowBalloonTip(1500, "Click here to see your screenshot!", "Saved to Documents/MyApp/Screenshot" & random & ".png!", ToolTipIcon.Info)
    baloon = pathh & "Screenshot" & random & ".png"




Aucun commentaire:

Enregistrer un commentaire