Results 1 to 6 of 6

Thread: [RESOLVED] File name generator

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    62

    Resolved [RESOLVED] File name generator

    Hi, im working for a school project and im trying to make a screenshot tool
    so i made it very simple with the following code below

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                Dim ScreenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
                Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
                Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(screenGrab)
                g.CopyFromScreen(New Point(0, 0), New Point(0, 0), ScreenSize)
                screenGrab.Save("C:\Screen.jpeg")
                MsgBox("Screenshot has been succesfully made!", MsgBoxStyle.Information, "Screenshot Success!")
            Catch ex As Exception
                MsgBox("Error", MsgBoxStyle.Critical, "Error!")
            End Try
        End Sub
    End Class
    so as you can see in my form only exists one button that takes the screenshot and then saves it in C:\ directory with the name SCREEN.JPEG
    but here's the problem i want to make it whenever i click the BUTTON to take the screeshot to generate a new name for the new screenshot (e.g. screen2.jpeg and so on and on...). I just want to not overwrite the first screenshot but to generate another name for the second screenshot.
    I'm sorry for my bad english...
    i would appreciate your help

    p.s
    i forgot to mention, I'm using Visual Basic 2008 Express Edition

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: File name generator

    Why don't you use unique names such as

    vb Code:
    1. Dim strFilename As String
    2. strFilename = Format(Now, "ddmmyyyyhhmmss")
    3. MsgBox strFilename & ".Jpeg"

    BTW is this vb.net? You might have to change it to vb.net code...
    Last edited by Siddharth Rout; Aug 21st, 2009 at 10:02 AM. Reason: Typo
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: File name generator

    try this:
    'from vbwire 11-19-08 if the filname exists it appends a number to the filename
    Code:
    Public Function GetUniqueFileName(ByVal FileName As String) As String
        Dim uniqueID As Long
        Dim sFile As String
        Dim sExt As Long, iExt As Integer
        
        
        sFile = Dir$(FileName, vbArchive)
        If sFile = vbNullString Then
            GetUniqueFileName = FileName
        Else
            iExt = InStrRev(FileName, ".")
            If iExt = 0 Then
                FileName = FileName & "[#]"
            Else
                FileName = Left$(FileName, iExt - 1) & "[#]" & Mid$(FileName, iExt)
            End If
            Do
                uniqueID = uniqueID + 1
                sFile = Dir$(Replace$(FileName, "#", uniqueID), vbArchive)
            Loop Until sFile = vbNullString
            GetUniqueFileName = Replace$(FileName, "#", uniqueID)
        End If
    
    End Function
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: File name generator

    Quote Originally Posted by jareck View Post
    I'm using Visual Basic 2008 Express Edition
    And that is why it should be in Visual Basic .NET rather than Visual Basic 6 And Earlier.

    Moved

  5. #5
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: File name generator

    You could always use IO.Path.GetTempFileName() to get a temp file name. Just make sure you clean them up when you are done with them.

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    62

    Re: File name generator

    hey thanks for your help everyone
    now i see how dump i am
    i just could use unique file names as koolsid said
    thnx again

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width