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;

Why don't you try using filestream. I pulled this code off a tutorial site, and changed it to append:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace write_file
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {


            // *** Write to file ***

            // Specify file, instructions, and privelegdes
            FileStream file = new FileStream("test.txt", FileMode.Append, FileAccess.Write);

            // Create a new stream to write to the file
            StreamWriter sw = new StreamWriter(file);

            // Write a string to the file
            sw.Write("Hello file system world!");

            // Close StreamWriter
            sw.Close();

            // Close file
            file.Close();


            // *** Read from file ***

            // Specify file, instructions, and privelegdes
            file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Read);

            // Create a new stream to read from a file
            StreamReader sr = new StreamReader(file);

            // Read contents of file into a string
            string s = sr.ReadToEnd();

            // Close StreamReader
            sr.Close();

            // Close file
            file.Close();


        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.