hevhev011717
hevhev011717

Reputation: 15

How can I automatically generate filenames before saving it in a folder VB .NET

I have a program that saves the image file path to the database. The image is then save to a specific folder where it is being retrieved by the file path saved in the database. However, I cannot save an image with the same filename because it is already in the folder.

Here is my code in copying the image into the folder:

Private Sub ButtonBrowseImage_Click(sender as System.Object, e As System.EventArgs) Handles ButtonBrowseImage.Click

     Dim opf as New OpenFileDialog
     opf.Filter="Choose Image(*.JPG, *.PNG, *.GIF)|*.jpg; *.png; *.gif"

       If opf.ShowDialog=Windows.Forms.DialogResult.Ok Then
         If opf.CheckFileExists Then
           Dim paths As String = Application.StartupPath.Substring (0, (Application.StartupPath.Length - 10 ))
           Dim CorrectFileName As String = System.IO.Path.GetFileName(opf.filename)
           System.IO.File.Copy(opf.FileName, paths + "\\artwork\\" + CorrectFileName)
         End If
       End If 

Is there any way to automatically generate filename or overwrite the saved image file with the same image but with a different filename?

Upvotes: 0

Views: 1026

Answers (1)

Naim Jamalludin
Naim Jamalludin

Reputation: 195

To generate random file name, you can use:

Path.GetRandomFileName()

Here is an example:

Imports System.IO

Module Module1

    Sub Main()
        Dim result = Path.GetRandomFileName()
        Console.WriteLine("Random file name is " + result)
    End Sub

End Module

' This code produces output similar to the following:

' Random file name is w143kxnu.idj
' Press any key to continue . . .

Upvotes: 1

Related Questions