我必须使用一组名称列表重命名数千个图像文件,例如名为 、 、 等的产品照片,topview
以及、、... 、、等。所以我想通过创建一个名为产品名称的特殊文件夹并将该产品的所有图像复制到其中来自动完成这项工作。我想像这样重命名此文件夹中的图像:每当我选择一张图片并按重命名(当然是缩略图视图)时,它都会出现一个预设名称列表的下拉列表(或对话框窗口等),例如、、,以及一个输入新名称的选项(如果列表中没有预设),如果在此之前拍摄过,那么下一次选择将创建新名称,然后,等等。topview1
topview2
sideview
sideview1
sideviewn
backview
frontview1234
F2topview
sideview
frontview
backview
topview
topview
topview1
topview2
topviewn
我不知道 Windows 通过按下 重命名文件的代码F2,所以我尝试另一种方法,我通过 Visual Studio 编写了一个基于 vb.net 的 Windows 应用程序窗体,但我的代码卡住了。请给我一些建议让它工作。我的代码包括运行代码和设计器代码,如下所示:
运行代码:
Imports System.IO
Public Class Form1
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If New Global.System.Windows.Forms.OpenFileDialog() With {
.Title = "Select a file",
.InitialDirectory = "D:\TestRen",
.Filter = "All files (*.*)|*.*|All files (*.*)|*.*",
.FilterIndex = 2,
.RestoreDirectory = True
}.ShowDialog() = Global.System.Windows.Forms.DialogResult.OK Then
Dim strFilename As String = New OpenFileDialog() With {
.Title = "Select a file",
.InitialDirectory = "D:\TestRen",
.Filter = "All files (*.*)|*.*|All files (*.*)|*.*",
.FilterIndex = 2,
.RestoreDirectory = True
}.FileName
'get extension
Dim extn As String = Path.GetExtension(strFilename)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim var As String
var = ComboBox1.Text
'It is stucking at this rename syntax
My.Computer.FileSystem.RenameFile(strFilename, var + extn)
End Sub
End Class
设计师代号:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.ComboBox1 = New System.Windows.Forms.ComboBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.Button3 = New System.Windows.Forms.Button()
Me.Button4 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'ComboBox1
'
Me.ComboBox1.FormattingEnabled = True
Me.ComboBox1.Items.AddRange(New Object() {"Front view", "Back view", "Left view", "Right view"})
Me.ComboBox1.Location = New System.Drawing.Point(15, 112)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(309, 21)
Me.ComboBox1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(370, 112)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Rename"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(370, 166)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(75, 23)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Next"
Me.Button2.UseVisualStyleBackColor = True
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(370, 239)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(75, 23)
Me.Button3.TabIndex = 3
Me.Button3.Text = "Cancel"
Me.Button3.UseVisualStyleBackColor = True
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(370, 57)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(75, 23)
Me.Button4.TabIndex = 6
Me.Button4.Text = "Select a file"
Me.Button4.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(488, 312)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.ComboBox1)
Me.Name = "Form1"
Me.Text = "Select a name"
Me.ResumeLayout(False)
End Sub
Friend WithEvents ComboBox1 As ComboBox
Friend WithEvents Button1 As Button
Friend WithEvents Button2 As Button
Friend WithEvents Button3 As Button
Friend WithEvents Button4 As Button
End Class
我的设计是这样的:单击按钮4(选择文件)在文件夹中选择一个文件名来输入旧名称和组合框新名字然后单击按钮 1(重命名)重命名文件,成功后,下一步按钮将循环获取下一个输入,完成后,取消按钮退出应用程序。需要更多代码才能获取所有目标,但我想了解关键过程(在 vb.net 中重命名)以进一步了解。