Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<<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
Posted
Updated 11-Mar-18 23:00pm
Comments
F-ES Sitecore 12-Mar-18 5:03am    
You can still use a timer in a console app, there is even an example in the documentation for Timer.

https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

You can then use FileInfo to create a file, or DirInfo to get all existing files from your directory if you need to work out the name of your new file.
BillWoodruff 12-Mar-18 11:28am    
I would have voted this #5 if you had posted as a solution.
F-ES Sitecore 12-Mar-18 11:46am    
CBA with the "can you write the code for me" responses to things like this :)

1 solution

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");
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900