Overriding an existing file in C#

2019-08-16 01:55发布

revised!

I want to create a file say called test.txt.

if that file already exists I want to create file called test1.txt and so on.

标签: c# file
3条回答
迷人小祖宗
2楼-- · 2019-08-16 02:17

The classes in System.IO should help you do that.

FileStream fs = System.IO.File.Create(fileName);
查看更多
Juvenile、少年°
3楼-- · 2019-08-16 02:26

Pass in FileMode.Create to File.Open(string, FileMode) when opening the file and it will create a new file every time.

FileStream file = File.Open("text.txt", FileMode.Create);
查看更多
smile是对你的礼貌
4楼-- · 2019-08-16 02:39

Here is a short example:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        String file = "text.txt"; 

        if (File.Exists(file))
            File.Delete(file);

        FileStream fs = File.Create(file);
    }
}
查看更多
登录 后发表回答