如何重命名VB.NET中的文件以具有唯一的后缀? [英] How to rename files in VB.NET to have a unique suffix?

查看:155
本文介绍了如何重命名VB.NET中的文件以具有唯一的后缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解如何在 VB.NET 中重命名文件,代码在我的帖子末尾。但是,我想知道是否可以重命名文件,如果该文件存在,然后重命名并将+1添加到文件名?

I understand how to rename a file in VB.NET as 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?

因此,如果我运行了

首次运行

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

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

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

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

更新

我决定而不是重命名和+1,最好只加上日期戳,这样对于像我一样挣扎的人来说:

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

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


推荐答案

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

You need to write your own logic for this.

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

The File class has many useful method for dealing with files.

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

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

The Path class has many methods for dealing with file paths.

Path.GetFileNameWithoutExtension(filePath)

这篇关于如何重命名VB.NET中的文件以具有唯一的后缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆