duplicate a text file

In my windows service I am creating a text file (filename is the current date) and dumping it into a specific folder. From there I want to duplicate that first file keep the original and create a new one with a specific name (lets call it copyoffile).
>>>>From there I want to copy that file from one folder to another (but thats not the question here).<<<<

So how do I duplicate the text file, assign it to a name of copyoffile.txt while maintaining the first copy of the file as well. Then the next time the service runs this (again the first file is created with the current date and time as the filename, but again the file is to be duplicated, called copyoffile.txt, but this file will and should overwrite the file from the previous time the service was run.
Any ideas?
lobosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Example:
System.IO.File.Copy(@"C:\Temp\Test.xml", @"C:\Temp\Copy of Test.xml");

Bob
0
lobosAuthor Commented:
Thanks but I am getting error message...

System.IO.File.Copy(@LogFile, @"C:\Temp\dd.txt");

The process cannot access the file "C:\Temp\dd.txt" because it is being used by another process.
0
Bob LearnedCommented:
That usually happens if you are creating the file, and not closing the file handle.  What creates the file in the first place?

Bob
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

lobosAuthor Commented:
LogFile = FlatFileLocation + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".txt";

using (FileStream fs = File.Create(LogFile))
{
DataSet dsFlat = SqlHelper.ExecuteDataset(trans, CommandType.StoredProcedure, spGET_NEW_TXNS);

for ( int x=0; x<dsFlat.Tables[0].Rows.Count;x++)
{                                    
string str = DateTime.Parse(dsFlat.Tables[0].Rows[x]["CartDate"].ToString()).ToString("yyyyMMdd");
//code to populate text file

      info = new UTF8Encoding(true).GetBytes(s);
      fs.Write(info, 0, info.Length);      
                        
}

info = new UTF8Encoding(true).GetBytes(h);
fs.Write(info, 0, info.Length);      

dsFlat.Clear();
dsFlat.Dispose();
fs.Flush();      
EventLog.WriteEntry(@LogFile + @"C:\temp\dd.txt");
System.IO.File.Copy(LogFile, @"C:\temp\dd.txt");
0
Bob LearnedCommented:
Try something like this, and see if it helps
using System.IO;
 
...
 
        private string BuildLogFile(SqlTransaction trans, string flatFileLocation)
        {
            string logFile = flatFileLocation + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".txt";
 
            // Get the data from the stored procedure, and build a list of 
            // Cart dates in year-month-date format.  Write that string to
            // the log file location.
 
            using (DataSet dsFlat = SqlHelper.ExecuteDataset(trans, CommandType.StoredProcedure, spGET_NEW_TXNS))
            {
                StringBuilder fileBuilder = new StringBuilder();
 
                foreach (DataRow dr in dsFlat.Tables[0].Rows)
                    fileBuilder.Append(DateTime.Parse(dr["CartDate"].ToString()).ToString("yyyyMMdd") + "\r\n");
 
                File.WriteAllText(logFile, fileBuilder.ToString());
 
                EventLog.WriteEntry(@LogFile + @"C:\temp\dd.txt");
 
                File.Copy(LogFile, @"C:\temp\dd.txt");
            }
            return logFile;
        }

Open in new window

0
lobosAuthor Commented:
I tried using some of your code....but I get this error as well
'System.IO.File' does not contain a definition for 'WriteAllText'

ALSO...
In my code sample I ommitted some of the details (//code to populate text file)....but I'm not sure how i can apply it using the code you posted...


LogFile = FlatFileLocation + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".txt"; 
 
using (FileStream fs = File.Create(LogFile)) 
{
DataSet dsFlat = SqlHelper.ExecuteDataset(trans, CommandType.StoredProcedure, spGET_NEW_TXNS);
 
for ( int x=0; x<dsFlat.Tables[0].Rows.Count;x++)
{    			
	string str = DateTime.Parse(dsFlat.Tables[0].Rows[x]["CartDate"].ToString()).ToString("yyyyMMdd"); 
 
	string s = "\r\n" + "\"" 
		+ dsFlat.Tables[0].Rows[x]["ROLL"].ToString() + "\"" 
		+ ", " + "\""
		+ this.GenerateCertificate(dsFlat.Tables[0].Rows[x]["Certificate"].ToString()) 
		+ "WEB"	+ "\"" 
		+", "+ "\"" 
		+ this.GetCertificateBalance(dsFlat.Tables[0].Rows[x]["TransactionID"].ToString()) 
		+  str + "\"" ;
 
	info = new UTF8Encoding(true).GetBytes(s);
	fs.Write(info, 0, info.Length);	
				
}
string h = "\r\n" + "\"" 
	+ "0000000000000000000" + "\"" 
	+ ", " + "\""
	+ "0000000"
	+ "WEB"	+ "\"" 
	+", "+ "\"" 
	+  "000000" + "\"" ;
 
info = new UTF8Encoding(true).GetBytes(h);
fs.Write(info, 0, info.Length);	
 
 
dsFlat.Clear();
dsFlat.Dispose();
fs.Flush();	
EventLog.WriteEntry(@LogFile + @"C:\temp\dd.txt");
System.IO.File.Copy(LogFile, @"C:\temp\dd.txt");	

Open in new window

0
Bob LearnedCommented:
I missed the 2003 part--that is 2005 code.

You have a using block, for the FileStream, and the code to copy needs to be outside of that block, since the stream won't be closed until the end of block is reached (the Dispose method is called).

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.