I have a small program that allows files to be uploaded, moved, and the evidence written to a text file.
However, I getting the following error:
I commented out the upload portion because it's not needed for testing, but the move portion should work fine. I have also moved the stw.close around a bit, and I get a few different errors. Where am I going wrong?
thanks in advance.
However, I getting the following error:
Code:
Error: The files have not been copied or archived.
System.ObjectDisposedException: Cannot write to a closed TextWriter. at System.IO.__Error.WriterClosed() at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) at System.IO.StreamWriter.Write(Char[] buffer, Int32 index, Int32 count) at System.IO.TextWriter.WriteLine(String value) at FileCopy.Program.Main(String[] args) in C:\Inetpub\[URL unfurl="true"]wwwroot\BIGFileCopy\FileCopy\FileCopy\Program.cs:line[/URL] 119
I commented out the upload portion because it's not needed for testing, but the move portion should work fine. I have also moved the stw.close around a bit, and I get a few different errors. Where am I going wrong?
Code:
static void Main(string[] args)
{
//declare variables
String sourcePath, targetPath, message, Subject;
string[] emailGroup = new string[3];
/*places recipient number in emailGroup array*/
string cis = "MY EMAIL HERE";
//Directory is the original directory
DirectoryInfo dir = new DirectoryInfo(@"\\SERVER1\Dealwe\BIG_Logs\");
//Define Directory Paths
sourcePath = @"\\SERVER1\Dealwe\BIG_Logs";
targetPath = @"\\SERVER1\Dealwe\BIG_Logs\Sent to RET";
FileInfo mi = new FileInfo(sourcePath);
DateTime currentDate = new DateTime();
currentDate = DateTime.Now;
string dateTime = Convert.ToString(mi.CreationTime.ToShortTimeString());
string fullTime = currentDate.ToString("Mdyy_hmm");
sendEmail confirmMail = new sendEmail();
//Write Log files
StreamWriter stw = new StreamWriter(@"\\SERVER1\Dealwe\TEST_BIG_TG\Sent to HGA\BIG_Logs_" + fullTime + ".txt");
string somefile = @"\\SERVER1\Dealwe\TEST_BIG_TG\Sent to HGA\BIG_Logs_" + fullTime + ".txt";
//Prepare Email Headers
Subject = "New File(s) Successfully Uploaded and Archived!";
message = "<font face='arial' size='2'>The following files were successfully uploaded to the FTP site and archived on the Server.<br><br><strong>Process Completion Time:</strong> " + currentDate + "</font><br>";
//Loop through files moved and write results to text file
try
{
//Find files by extension
String[] files = Directory.GetFiles(sourcePath, "*.edi");
for (int i = 0; i < cis.Length; i++)
{
if (files.Length > 0)
{
//Write to Stream
stw.WriteLine("BIG Files successfully uploaded to FTP site and archived on the Server. Post Time: " + currentDate);
stw.WriteLine("");
stw.WriteLine("");
foreach (String file in files)
{
//Check if target folder exists. If it doesn't, create a new target folder.
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
else
{
//Grab Basic file information
String filename = Path.GetFileName(file);
FileInfo fu = new FileInfo(filename);
string afile = fu.Name;
String dest = Path.Combine(targetPath, filename);
/*PRODUCTION FTP Info here*/
/*comment out for testing*/
/*uploadFile myfi = new uploadFile();
myfi.Upload(ftpaddress, un, pw, file);
*/
//Move the file on the same server to the Destination Folder
System.IO.File.Move(file, dest);
//Write Information to Text File
stw.WriteLine("*" + filename);
stw.WriteLine("");
//Write the same information to email
message += "<font face='arial' size='2'><ul><li>" + filename + "</li></ul></font><br>";
stw.Close();
}
}
confirmMail.send(message + "<br><br>", cis, Subject, somefile);
}
else
{
//Send email if there are no files on the server
confirmMail.send("<font face='arial' size='2'>There were no files on the Dealwe server to send. Process will repeat in an hour.</font>" + "<br><br>", cis, "No Files to Process", "0");
}
//stw.Close();
}
}
catch (Exception err)
{
confirmMail.send("<font face='arial' size='2'>Error: The files have not been copied or archived. <br></font>" + err, cis, "Error Processing Files", "0");
}
}
}
thanks in advance.