Hi everyone...(read carefully)

I am Developing an application in C#.
In which First I create two text files say a.text and b.text.
and then fetch data from them ......

Now every day when I run my code for the first time I want to delete these files and recreate new version . This should happend only first time.

All the next executions shall use today's newly created files. No deleting of files again for the rest of the day..

NOTE - Deletion and creation of files is only once in a day !!!

same task should happen everyday

Any Ideas ??????? or Thoughts??????

You an use System.IO.File.GetCreationTime(filename) to get when a particular file was created. Compare that to today's date and delete as necessary. You can then open a file with append access via a StreamWriter to write to a file. If the file exists, it will merely be appended to. If not, it will be created. Sample below.

string filename = @"C:\yourfilepath_and_name.txt";
        DateTime fileDate = File.GetCreationTime(filename);

        if (fileDate.Date < DateTime.Now.Date)
        {
            File.Delete(filename);
        }

        StreamWriter writer = new StreamWriter(filename, true); // opens file for appending
        writer.WriteLine("write to the file");

        // finish processing, then close and dispose of object
        writer.Close();
        writer.Dispose();

Compile your code to an executable, create a scheduled task in your operating system, and off you go.

I like your idea apegram!!!. the only thing is that I will exclude the date check since you can schedule windows to run daily in a specific time, i do not see the need to make date check.

I like your idea apegram!!!. the only thing is that I will exclude the date check since you can schedule windows to run daily in a specific time, i do not see the need to make date check.

His description indicated the program could execute many times in a given day. The date check ensures the file is only deleted the first time. If it turns out the program only executes a single time, then of course parts of the code become superfluous.

Oh i thought the running code in the windows schedule task was separately from the real program. Got it.

You an use System.IO.File.GetCreationTime(filename) to get when a particular file was created. Compare that to today's date and delete as necessary. You can then open a file with append access via a StreamWriter to write to a file. If the file exists, it will merely be appended to. If not, it will be created. Sample below.

string filename = @"C:\yourfilepath_and_name.txt";
        DateTime fileDate = File.GetCreationTime(filename);

        if (fileDate.Date < DateTime.Now.Date)
        {
            File.Delete(filename);
        }

        StreamWriter writer = new StreamWriter(filename, true); // opens file for appending
        writer.WriteLine("write to the file");

        // finish processing, then close and dispose of object
        writer.Close();
        writer.Dispose();

Compile your code to an executable, create a scheduled task in your operating system, and off you go.

Wouldn't "if (fileDate < DateTime.Now.Date)" work as well?
Or "if (File.GetCreationTime(filename) < DateTime.Now.Date)"?

Wouldn't "if (fileDate < DateTime.Now.Date)" work as well?
Or "if (File.GetCreationTime(filename) < DateTime.Now.Date)"?

Certainly. Inline the code as you see fit, and indeed fileDate.Date is overkill (versus fileDate) and could very well be optimized away.

You an use System.IO.File.GetCreationTime(filename) to get when a particular file was created. Compare that to today's date and delete as necessary. You can then open a file with append access via a StreamWriter to write to a file. If the file exists, it will merely be appended to. If not, it will be created. Sample below.

string filename = @"C:\yourfilepath_and_name.txt";
        DateTime fileDate = File.GetCreationTime(filename);

        if (fileDate.Date < DateTime.Now.Date)
        {
            File.Delete(filename);
        }

        StreamWriter writer = new StreamWriter(filename, true); // opens file for appending
        writer.WriteLine("write to the file");

        // finish processing, then close and dispose of object
        writer.Close();
        writer.Dispose();

Compile your code to an executable, create a scheduled task in your operating system, and off you go.

Hi Man.....Thanks a lot!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.