troubleshooting Question

Overwrite oldest document

Avatar of dekempeneer
dekempeneerFlag for Belgium asked on
.NET ProgrammingC#Microsoft Development
14 Comments1 Solution499 ViewsLast Modified:
I have now some code that saves the document. But if a document exist it will say it already exists blablabla.
So I changed my code as in the example that it would add 1 number to the document. This (not tested yet) will work I pressume, but I will still have infinite documents in the folder and I don't want that.
My question is, how can I make it work so that when I have eg 10 documents in the folder it will overwrite the oldest and from then on it will each time overwrite the oldest document. This way there will only be 10 documents in the folder and the 10 last one are still there ??
private void SaveDocument()
        {   Random random = new Random();
            int docnr = random.Next(10);
            while (File.Exists(@"C:\Templates\NewMemo" + docnr + ".docx"))
            {
                docnr++;
            }
            object missing = System.Reflection.Missing.Value;
            object fileName = @"C:\Templates\NewMemo" + docnr + ".docx";
            Globals.ThisDocument.SaveAs(ref fileName,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing);

        }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 14 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros