如何在C#中自动重命名文件 [英] How to rename a file automatically in C#

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

问题描述

我必须创建多个文本文件。



如果已经存在,我需要在文件名后添加计数。



例子



如果我输入test.txt



如果文件存在那么它应该重命名为test(1).txt。



如果我再次给test.txt它应该重命名为test(2).txt。



如何使用C#

I have to create multiple text files.

If the already exists I need to add count after the filename.

Example

If I Enter test.txt

If file exists then it should rename as test(1).txt.

If I again give test.txt it should rename as test(2).txt.

How to do this using C#

推荐答案

使用
创建文件名
Create the filename with
string filename = System.IO.Path.Combine(folder, string.Format("Test({0}).txt", i));



并检查它的存在与


and check for its existence with

System.IO.File.Exists(filename);



,直到返回false。


till that returns false.


这篇关于如何在C#中自动重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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