如何在VB.NET中重命名文件 [英] How to rename file in VB.NET

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

问题描述

我知道如何为我在用重命名文件中 VB.NET code在帖子的末尾。不过,我想知道是否有可能重命名一个文件,如果文件存在,然后将其重命名,并添加+1到文件名?

所以,如果我跑了code。

运行它第一次

  My.Computer.FileSystem.RenameFile(C:\测试\ test.txt的,C:\测试\ NewName.txt)
 

再次运行它,但它应该添加+1作为文件将已经存在,所以它应该是C:\测试\ NewName1.txt

  My.Computer.FileSystem.RenameFile(C:\测试\ test.txt的,C:\测试\ NewName.txt)
 

更新

我决定,而不是重命名和+1,它会更好,只是日期戳,所以任何人都像我一样谁的斗争:

  My.Computer.FileSystem.RenameFile(C:\测试\ test.txt的,测试和放大器;格式(Date.Now,DDMMYY)及。 TXT)
 

解决方案

您需要编写自己的逻辑这一点。

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

 如果File.Exists(文件路径)然后
  给一个新名称
其他
  使用现有名称
结束如果
 

借助 路径 类有许多方法来处理文件路径。

  Path.GetFileNameWithoutExtension(文件路径)
 

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?

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"

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:

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天全站免登陆