如何在C#中每1分钟创建一个新的文本文件 [英] How to create new text file every 1 min in C#

查看:556
本文介绍了如何在C#中每1分钟创建一个新的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<<我想创建一个程序控制台应用程序,每1分钟后创建一个文本文件。就像在c#中使用计时器一样,它创建一个文件名为file.txt,类似地在1分钟后它应该创建一个file1.txt然后在1分钟之后它应该创建一个file2.txt,就像每1分钟一样它创建新的文本文件



我尝试过:



使用流编写器或文件文本请指导我

<<I want to create a program console application that create text file after each 1 min. Like using timer in c# and it create a file name as "file.txt" similarly after 1 min it should create a "file1.txt" and then after 1min agian it should create a "file2.txt" like every each 1 min it create new text file

What I have tried:

Using stream writer or file text pls guide me

推荐答案

您需要一个计时器:

You need a Timer:
static void Main(string[] args)
    {
    System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(DoSomething));
    timer.Change(0L, 60000);
    Console.ReadLine();
    }
private static int fileNo = 1;
private static string strPath = @"D:\Temp\file";
private static void DoSomething(object state)
    {
    File.WriteAllText(strPath + (fileNo++).ToString() + ".txt", "The text to write");
    }


这篇关于如何在C#中每1分钟创建一个新的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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