如果文件存在则增加文件名使用c# [英] increment filename if file exists. using c#

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

问题描述

我有一个任务,我必须将文件保存在同一个文件夹中。如果该名称已经存在文件名,那么我想给出相同的名称并将该新文件添加为1,2,3 ....等等,如备份(1),备份(2),备份(3) ....等等。我以编程方式想要它。

I have one task in that i have to save files in same folder. If file name is already exists by that name then i want to give same name and add that new file as 1,2,3....& so on, Like Backup(1), Backup(2), Backup(3)....& so on. I want it programatically.

推荐答案

尝试这样做,你可能需要做一些改变,但这个逻辑会起作用

< br $>
Try to do like that, you might need to do some change, but this logic will work

do
{
    fileCount++;
}
while (System.IO.File.Exists("FileName" + (fileCount > 0 ? "(" + fileCount.ToString() + ")" : "")));

            //Not create a file
System.IO.File.Create("FileName" + (fileCount > 0 ? "(" + (fileCount + 1).ToString() + ")" : ""));


试试这个代码



try this code

private void button1_Click(object sender, EventArgs e)
       {
           string fileName = "C:\\Backup";
           int count = 0;
           Find :
           if (File.Exists(fileName))
           {
               fileName = fileName + "(" + count.ToString() + ").txt";
               count++;
               goto Find;
           }
           else
           {
               //Add your logic here
               File.Create(fileName);
           }
       }


你好亲爱的

有一种方法可以确定是否有文件一个目录

Hello Dear
there is a method which determines if there is a file in a directory
System.IO.File.Exists("FilePath")



你可以像这样使用它br />


you can use it like this

if (System.IO.File.Exists("FilePath"))
{
}
else
{
}


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

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