Binary output or replace the output

Sep 2, 2009 at 9:33 AM


I am working on a template which generate strong enum for images in a directory.
I tried to copy the images to a project's directory and add BuildAction="EmbeddedResource".
Everything is ok, but at the end when I tried to Copy the image to the output file
the result is zero length. I think the template generate the output after I copied the file.
How can I call template.CopyImage() after template generation?


This is my dummy template:

public class ImageDummyTemplate : Template
	protected override void RenderCore()
	public void CopyImage(string path, string imageFolder)
		string dst = Path.Combine(GetFullPath(), imageFolder);
		dst = Path.Combine(dst, Path.GetFileName(path));
		File.Copy(path, dst, true);

	public string GetFullPath()
		string output =  string.IsNullOrEmpty(Output.Project) ? @".\" : Output.Project;
		string baseFile = TransformationContext.Host.TemplateFile;
		string baseDirectory = Path.GetDirectoryName(baseFile);
		string fullPath = Path.Combine(baseDirectory, output);
		return Path.GetDirectoryName(Path.GetFullPath(fullPath));

and this is my Generator:
public class ImageGenerator : Generator
	public List<string> projects = new List<string>();
	protected override void RunCore()
			var template = new ImageDummyTemplate();
			template.Output.BuildAction = "EmbeddedResource";
			string path = @"s:\Images\";
			string imageFolder = @"Generated.Images\";
			var files = Directory.GetFiles(path, "*.*");
			foreach (var file in files)
				string output = Path.Combine(imageFolder, Path.GetFileName(file));
				template.Output.Project = string.Empty;
				template.CopyImage(file, imageFolder);
				foreach (var project in projects)
					template.Output.Project = project;
					template.CopyImage(file, imageFolder);
Sep 2, 2009 at 11:24 AM

Does your code generator need to produce a single enum file or copy image files to another directory? Or does it need to do both?

Sep 2, 2009 at 1:04 PM

Sorry for confusion, the enum part is ready and works, I would like to want to "import" graphics file into project like "EmbeddedResource". So the code above generate a lot of file with image file name into destination folder (which works good) but zero length only. I suppose the template overwrite my image after the template.CopyImage.


Sep 2, 2009 at 4:39 PM

Hmm.... So, does your code generator generate these image files? Or, do you have an existing set of image files that you want to automatically add to another project?

Sep 2, 2009 at 5:07 PM

I have an existing set of image files in a directory. My intention during enum generator I would like to add image as "EmbeddedResource" to the projects. This will be convinient and safe, the resource and the enum will be always in sync. So I made two templates. First one for enums, this is ready and works, and a second "dummy" for add image files to projects. This is almost working, it is add image file to project as "EmbeddedResource" but I cannot move the binary content. It is always zero length.

Sep 4, 2009 at 5:11 AM

I solved. The solution is rather ugly, but works: my original solution has only 1 Generator template, which generates all files into multiple projects.
Now I striped the image copying into an independent template. The generator template insert image file name into projects as "EmbeddedResource", and generate zero length image file. If the template's name is after the generator's name on alphabet then the Visual Studio will run after it. The new template simple overwrite the image files with the binary content.

I think it will be usefull if the template support binary content somehow.

Sep 4, 2009 at 10:32 AM

It may be possible to create such a solution. However, this changes the fundamental purpose of the toolkit - it's not called "binary template transformation toolkit" after all.

So these images, they are not "generated" right? They are simply copied from one folder to another? Why not simply put all these images to the destination folder to begin with?

Sep 4, 2009 at 11:05 AM
Edited Sep 4, 2009 at 11:07 AM

Yes it is only copied, but this is not required, but I cannot find other solution.
The best will be if I can add image file (or any file in a template) to projects without copying...
But now if I tried the text template overwrite my binary files.

I have more projects. When I add/modify/delete an image in the image folder then
it is safe if this automaticly added to all projects and generated the proper ENUM value.

For hand copying: after copying you should update the "EmbeddedResource" property,
so it is not convenient.

Sep 4, 2009 at 8:51 PM
Edited Sep 4, 2009 at 9:44 PM

Would it be possible for you to embed these images as resources in a single DLL and then load these resources from all other assemblies that need these images?

Sep 5, 2009 at 5:24 AM

My projects are mixed (compact framework and full framework). AFAIK I cannot make shared DLL for both framework.