All:
I am trying, without much success, to append files in a C#.Net application. The code below shows my most recent attempt, unfortunately it doesn't work (this is iteration 5296.5 - approximately).
Coming into this case statement there are two arguments, tokens[0] and tokens[1], which I have read and parsed out from the command line. The tokens[0] value is the file name of the file to be appended while tokens[1] is the file name of the output file, i.e., the one to which tokens[0] is appended.
Also, FYI, the LogIt call is to a routine which appends one line at a time to an error log file (and which works perfectly I might add :) )
This has become somewhat of a show stopper for my team so I've finally decided to stop dithering around and ask for assistance.
Thanks in advance for comments/guidance/pointers - I appreciate it.
Tom
case "APPEND": // Append one file to another
try
{
string cdir = Directory.GetCurrentDirectory();
string toFile = cdir + "\\" + tokens[1];
string fromFile = cdir + "\\" + tokens[0];
if (!File.Exists(toFile))
{
FileInfo t = new FileInfo(toFile);
StreamWriter t2 = t.CreateText();
t2.Close();
// t2.Dispose();
}
string[] dlist = Directory.GetFiles(Directory.GetCurrentDirectory());
Wildcard wcard = new Wildcard(tokens[0]);
foreach (string f in dlist)
{
if (wcard.IsMatch(tokens[0]))
{
using (StreamReader sr = File.OpenText(f))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
using (StreamWriter sw = File.AppendText(toFile))
{
sw.WriteLine(s);
}
}
sr.Close();
sr.Dispose();
}
System.Threading.Thread.Sleep(5000);
LogIt("File " + fromFile + " appended to " + tokens[1]);
}
}
}
catch (Exception ex)
{
LogIt("ERROR : APPEND cmd error: " + ex.Message);
errorFlag++;
error_messages[++emindx] = "APPEND cmd error - " + ex.Message;
}
break;