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?
 
Bob LearnedConnect With a Mentor Commented:
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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
All Courses

From novice to tech pro — start learning today.