I’m trying to write out to two files at once using C#. The location of the secondary folders is constantly changing and I may need to have the output write out to three or more folders at once. What is the best way to do this?
I currently have the function Output(string s) which uses StreamWriter to write to a specific static location. I’m thinking I could add a syncFolder(string[] folderPaths) function at the end of Output which would update the contents (folders and textfiles) of Folder 2,3,4,… to look exactly like those of Folder 1.
Is this the best way to do this? If so, how would I sync the folders?
You could use an approach like various tracing and logging libraries do, where you have data source classes, data sink classes, and bind them together with a third set of configuration/layout/renderer classes.
In relational terms, this would enable a many-to-many relationship.
Stream readers and writers would work for this, but I believe they are designed to be coupled 1-to-1.
A design pattern that you would want to look into is the Observer Pattern. In C#, this is implemented with events. This might not be the fastest solution in the world (due to delegate calls on every invocation of the source), but it certainly would be the most flexible. It also should be adaptable to asynchronous sink draining.