从文件夹中获取最新的“x.png”

时间:2015-09-25 04:54:15

标签: vb.net random directory screenshot

我的程序中有一个“截屏”功能。我正在截取屏幕截图并使用以下代码保存:

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

目录是文件。随机就是这个

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

但我不想这样保存。我想按顺序保存。我想检查文件夹,获取最新名称并为其添加+1。如果最新的是Screenshot-17.png,请将其另存为Screenshot-18.png。

我正在考虑子字符串,但我不知道如何从文件夹中获取最新的png文件。

完整代码在这里:

    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"

2 个答案:

答案 0 :(得分:2)

你可以试试这个:

CsvParserSettings settings = new CsvParserSettings();
CsvParser parser = new CsvParser(settings);
List<String[]> allRows = parser.parseAll(new FileReader(new File("/path/to/your.csv")));
for(String[] row : allRows){
     if(isHeaderRow(row)){ // isHeaderRow() would test whether each element is enclosed within $
          // do stuff to "switch" to a new header row.
     } else {
          // do stuff
     }
}

答案 1 :(得分:0)

此方案的更好选择是:

Dim newFileNMame As String = (Date.Today.ToString("yyyy-MM-dd") & "-" & TimeOfDay.ToString("HH-mm-ss"))
Bmp.Save(dir & "\MyApp\Screenshot\" & newFileNMame & ".png")

或者您将使用以下代码获取最后一个文件名:

   Dim x As DirectoryInfo = New DirectoryInfo("D:\\TestFolder")
   Dim lastfileName = x.GetFileSystemInfos().OrderByDescending(Function(f) f.CreationTime).FirstOrDefault().Name