如何在VB.NET中重命名文件

编程入门 行业动态 更新时间:2024-10-24 14:24:03
本文介绍了如何在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中重命名文件

本文发布于:2023-11-04 05:24:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557062.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重命名   文件   如何在   NET   VB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!