Reputation: 13626
I copy image files from one folder to another with help of File.Copy()
method.
I need to give unique file name to the copied image file before it is pasted to the folder.
Any idea how I can implement this?
Upvotes: 7
Views: 21813
Reputation: 5813
I recommend using Path.GetRandomFileName()
.
The previously accepted answer was to use Path.GetTempFileName
which creates a file. This is not ideal when that file is going to be the target of a file copy operation, as stated in the question.
Unlike GetTempFileName, GetRandomFileName does not create a file. When the security of your file system is paramount, this method should be used instead of GetTempFileName.
SOURCE: https://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename(v=vs.110).aspx
EXAMPLE:
Imports System.IO
Module Module1
Sub Main()
Dim fileName = Path.GetRandomFileName()
Console.WriteLine("Random file name is " & fileName)
End Sub
End Module
' This code produces output similar to the following:
' Random file name is w143kxnu.idj
Upvotes: 3
Reputation: 27342
Eyossi's answer should be the accepted solution but another alternative:-
You could use a guid to create a unique name ("Such an identifier has a very low probability of being duplicated."):
Dim filename As String = Guid.NewGuid().ToString + ".png"
Upvotes: 6
Reputation: 155
Here's another way to do it
fileName = User_Appended_File_Name & "_" & DateTime.Now.ToString("yyyyMMdd_HH_mm_ss")
It would create a fairly identifiable File Name, with the added advantage of knowing when it was created, a big ol' timestamp.
edit: It will not work if the file saving rate is faster than 1second.
Upvotes: 4
Reputation: 4340
You can use Path.GetTempFileName
more info at: http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename(v=vs.100).aspx
a quote from the link:
"Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file."
Upvotes: 5