如果文件已经存在,如何增加文件名 [英] How to increment the filename if file already exists

查看:54
本文介绍了如果文件已经存在,如何增加文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的vb.net winform应用程序中,我正在将文件(例如:sample.xls从一个文件夹移动到另一个文件夹.如果已经存在相同名称的文件,则应增加新文件名(例如:sample(1).xls).我该如何实现?

In my vb.net winform application, I am moving the file (ex: sample.xls from one folder to another. If file already exists with the same name, the new file name should be incremented (ex:sample(1).xls). How can i acheive this?

推荐答案

这是一个漂亮的过程式"答案:

Hi here's a pretty "procedural" answer:

Dim counter As Integer = 0

Dim newFileName As String = orginialFileName

While File.Exists(newFileName)
    counter = counter + 1
    newFileName = String.Format("{0}({1}", orginialFileName, counter.ToString())
End While

您将需要对System.IO的import语句

you will need an imports statement for System.IO

这篇关于如果文件已经存在,如何增加文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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