如何在 VB.NET 中重命名文件以具有唯一后缀?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10784613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:26:17  来源:igfitidea点击:

How to rename files in VB.NET to have a unique suffix?

.netvb.net

提问by HymanSparrow

I understand how to rename a file in VB.NETas I use in the code at the end of my post. However, I was wondering if it's possible to rename a file and if the file exists then to rename it and add +1 to the file name?

我了解如何在VB.NET 中重命名文件,就像我在文章末尾的代码中使用的那样。但是,我想知道是否可以重命名文件,如果文件存在,然后重命名它并将 +1 添加到文件名?

So if I ran the code.

所以如果我运行代码。

'Run it first time

'第一次运行

My.Computer.FileSystem.RenameFile("c:\test\test.txt", "c:\test\NewName.txt")

'Run it again, but it should add +1 as the file will already exists, so it should be "c:\test\NewName1.txt"

'再次运行它,但它应该添加+1,因为文件已经存在,所以它应该是“c:\test\NewName1.txt”

My.Computer.FileSystem.RenameFile("c:\test\test.txt", "c:\test\NewName.txt")

Update

更新

I decided rather than rename and +1, it would be better to just date stamp it, so for anyone who struggles as I did:

我决定与其重命名和 +1,不如给它加个日期戳,所以对于像我一样挣扎的人:

My.Computer.FileSystem.RenameFile("c:\test\test.txt", "Test" & Format(Date.Now, "ddMMyy") & ".txt")

回答by Oded

You need to write your own logic for this.

您需要为此编写自己的逻辑。

The Fileclass has many useful method for dealing with files.

File类有处理文件的许多有用的方法。

If File.Exists(filePath) Then
  ' Give a new name
Else
  ' Use existing name
End If

The Pathclass has many methods for dealing with file paths.

Path类有处理文件路径的许多方法。

Path.GetFileNameWithoutExtension(filePath)

回答by Lloyd Powell

If System.IO.File.Exists("c:\test\NewName.txt") Then
   ' add +1 or loop exists with increment on the end until file doesn't exist
End If

回答by Jabbar

You don't need to mention the complete file path in newFileNameparameter, just mention new file name here otherwise you will get ArgumentException.

您不需要在newFileName参数中提及完整的文件路径,只需在此处提及新文件名,否则您将获得ArgumentException.

Dim filePath As String = "C:\fingerprint1"

If File.Exists(filePath) Then

    Dim strNewFileName As String = "Fingerprint221"

    My.Computer.FileSystem.RenameFile(filePath, strNewFileName)

 End If

回答by sober

Another simple way to rename a file is to use the Move() method of System.IO.File.

另一种重命名文件的简单方法是使用 System.IO.File 的 Move() 方法。

Example:

例子:

System.IO.File.Move("C:\temp\file1.txt", "C:\temp\file1_renamed.txt")

回答by Yusuf Efendy

   Public Sub RenameFile(ByRef FileFind As String, ByRef NewReplaceFileName As String)
        Dim Ada As String = Path.GetFileNameWithoutExtension(FileFind)
        'VS2013 Dim Ada As String = File.Exists(FileFind)
        If Ada.Length > 0 Then
            My.Computer.FileSystem.RenameFile(FileFind, NewReplaceFileName)
            Exit Sub
        Else
            MsgBox("File doesn't exists")
        End If
    End Sub