Have a slight issue that I need a second pair of eyes because I am at the point where the code is just being hacked apart instead of taking time needed to break it down.
The end goal of the program is to read in X number of files depending on users choices, take those files put them into memory all together, and then append them all to one Word file.
It is only writing out the first file. If I change to a WriteFile
method it only writes out the last file. I would like it to write out all files
Any tips in the right direction would be appreciated.
private void cmdSubmit_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "Word Files (*.doc)|*.doc";
saveFile.FilterIndex = 1;
saveFile.RestoreDirectory = true;
string dirPath = "";
FileStream destStream = (dynamic)null;
MemoryStream outStream = new MemoryStream();
if (saveFile.ShowDialog() == DialogResult.OK)
{
dirPath = saveFile.FileName;
destStream = new FileStream(dirPath, FileMode.Create, FileAccess.Write);
destStream.Close();
}
DirectoryInfo di = new DirectoryInfo(@"FilePath");
FileInfo[] args = di.GetFiles("*.doc");
foreach (object itemChecked in chkLB.CheckedItems)
{
//loop through checked items and add to file
string fileStream = di.ToString() + itemChecked.ToString() + ".doc";//READ IN FILES
FileStream inFile = File.OpenRead(fileStream); //Read the files one by one
outStream.SetLength(inFile.Length);
inFile.Read(outStream.GetBuffer(), 0, (int)inFile.Length);
outStream.Flush();
inFile.Close();
fileSave(outStream, dirPath);
//MessageBox.Show("Item with title: \"" + itemChecked.ToString()); Nice trick to add "" around a value
}
//Open File
//Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
//Document openDoc = word.Documents.Open(dirPath);
MessageBox.Show("DONE");
}
public static void fileSave(MemoryStream memStream, string saveFile)
{
//FileStream fileOut = File.Open(saveFile, FileMode.Append);
if (!File.Exists(saveFile))
{
FileStream fileOut = new FileStream(saveFile, FileMode.OpenOrCreate, FileAccess.Write);
//stream writter?
memStream.WriteTo(fileOut);
fileOut.Flush();
fileOut.Close();
}
else
{
FileStream fileOut = new FileStream(saveFile, FileMode.Append, FileAccess.Write);
memStream.WriteTo(fileOut);
fileOut.Flush();
fileOut.Close();
}
}