C#
My lead programmer wants me to use the code below to write out the records on a file.
The problem is that if the file is already existing them it does not delete the old file and create a new file and only appends.
I was doing it another way and he did not like it because it was like VB.
Is there way to check for an existing file and delete it with this "using" statement. I know this seem dumb, but that is what he wants.
public static void Printweekly(string Output)
{
using (StreamWriter weekly = new StreamWriter(Program.location + "\\" + Program.name + Program.weeklyEndDate + ".csv", true))
{
weekly.WriteLine("{0}", Output);
}
}
Can anyone help? Thanks ahead of time.
arep
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 5, 2011, 1:22 PM EDT
Zoran
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 5, 2011, 12:56 PM EDT
A RepaskyRank #8,108 · 228 reputationPosted Jul 5, 2011, 1:09 PM EDT
arep
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 5, 2011, 1:13 PM EDT
public static void Printweekly(string Output, bool append)
Zoran
A RepaskyRank #8,108 · 228 reputationPosted Jul 5, 2011, 1:19 PM EDT
Thanks,
arep
A RepaskyRank #8,108 · 228 reputationPosted Jul 6, 2011, 8:14 AM EDT
Thanks, it does work the way I want it now. I already knew that it will open and close it everytime in the loop. I was opening it with a FileStream as below, but the Lead Programmer did not want it. He wants me to use the new feature of .NET the using statement. In fact, he was over my shoulder telling me what to do. So I thank you for your help. In testing the programs, it was getting irratible to have to go in and delete the file or files everytime. I am going to end up with a lot of programs that do the same thing. So thanks for the help.
arep
Sam HobbsRank #107 · 20.5K reputationPosted Jul 6, 2011, 12:46 PM EDT
The C# language and .Net are designed to use garbage collection. The initial design of C# did not have the IDispose interface; it was added during beta testing of C#. So initially there was no need to ensure that an object is disposed of. The "using" statement was created to ensure that IDispose is called and to ensure that any other relevant cleanup is done. The "using" statement seems inelegant to me and it is a kluge; I agree that it is part of C# that is not designed as well as it could be. There are many other things about C# that are designed well.
VulpesRank #55 · 29.2K reputationPosted Jul 6, 2011, 1:41 PM EDT