Overriding an existing file in C#

Overriding an existing file in C#

彩扇题诗 发布于 2021-11-27 字数 155 浏览 950 回复 3 原文


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.

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。



需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。


梦里泪两行 2022-06-07 3 楼

Here is a short example:

using System;
using System.IO;

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

        if (File.Exists(file))

        FileStream fs = File.Create(file);
朮生 2022-06-07 2 楼

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

FileStream fs = System.IO.File.Create(fileName);
心是晴朗的。 2022-06-07 1 楼

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);