当我使用openfiledialog加载图像时,我需要使用相同的文件名和相同的扩展名将图像的备份副本存储到另一个文件夹。所以我如何做到这一点,如下面提到的代码我能够复制只有一个图像,我给了该图像的随机字符串。但我不需要那个。我想用相同的文件名和相同的扩展名进行复制。如果我有相同的文件名,它应该覆盖它但不能用不同的名称和扩展名。
非常感谢任何帮助。
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
OpenFileDialog1.Multiselect = True
Dim r As New Random()
Dim i As Integer
Dim strTemp As String = ""
For i = 0 To 8
strTemp = strTemp & Chr(CInt(Int((26 * r.NextDouble()) + 65)))
Next
Dim str As String
For Each str In OpenFileDialog1.FileNames
System.IO.File.Copy(str, Application.StartupPath + "\DownloadedImages\" & "strTemp.jpg", True)
Next
End If
我实现了它,但有一个问题我可以复制一个图像你能说我怎么做多个图像。这里是代码:
Dim fso As New FileSystemObject
Dim str As String
str = OpenFileDialog1.FileName
MyExtension = fso.GetExtensionName(str)
For i = 0 To OpenFileDialog1.FileNames.Length - 1
System.IO.File.Copy(OpenFileDialog1.FileNames(i), Application.StartupPath + "\DownloadedImages\" + strTemp & "." & MyExtension, True)
Next
答案 0 :(得分:1)
这一行:
System.IO.File.Copy(str, Application.StartupPath + "\DownloadedImages\" & "strTemp.jpg", True)
应阅读:
System.IO.File.Copy(str, Application.StartupPath + "\DownloadedImages\" & strTemp & ".jpg", True)
答案 1 :(得分:0)
您应该使用Path课程。它有获取带或不带扩展名的文件名的方法以及仅获取扩展名。
另外,strTemp
不应该在引号中,因为这将是文字字符串"strTemp"
而不是变量strTemp
答案 2 :(得分:0)
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim str As String
For Each str In OpenFileDialog1.FileNames
Dim fso As New FileSystemObject
Dim MyName As String
Dim MyExtension As String
MyName = fso.GetFileName(CStr(str))
MyExtension = fso.GetExtensionName(MyName)
System.IO.File.Copy(str, Application.StartupPath + "\Backup\" + MyName & "." & MyExtension, True)
CheckedListBox1.Items.Add(str, CheckState.Checked)
Thumbcontrol1.AddThumbnail(str)
Thumbcontrol1.BackgroundImage = Nothing
CheckedListBox1.SelectedIndex = 0
Next