Create designer.cs file from ResXRersourcewriter generated resource file

If the file is added to a Visual Studio Project you have to set the Custom Tool property of the .resx file to ResXFileCodeGenerator. Then will VS automatically create the needed designer file.

In one project I made a T4 script that scans the folder within the project for all images and let create a corresponding ressource file at a click.

Here is the needed part out of the T4 script:

var rootPath = Path.GetDirectoryName(this.Host.TemplateFile);

var imagesPath = Path.Combine(rootPath, "Images");
var resourcesPath = Path.Combine(rootPath, "Resources");

var pictures = Directory.GetFiles(imagesPath, "*.png", SearchOption.AllDirectories);

EnvDTE.DTE dte = (EnvDTE.DTE)((IServiceProvider)this.Host)
                   .GetService(typeof(EnvDTE.DTE));

EnvDTE.Projects projects = dte.Solution.Projects;
EnvDTE.Project iconProject = projects.Cast<EnvDTE.Project>().Where(p => p.Name == "Icons").Single();
EnvDTE.ProjectItem resourcesFolder = iconProject.ProjectItems.Cast<EnvDTE.ProjectItem>().Where(item => item.Name == "Resources").Single();

// Delete all existing resource files to avoid any conflicts.
foreach (var item in resourcesFolder.ProjectItems.Cast<EnvDTE.ProjectItem>())
{
    item.Delete();
}

// Create the needed .resx file fore each picture.
foreach (var picture in pictures)
{
    var resourceFilename =  Path.GetFileNameWithoutExtension(picture) + ".resx";
    var resourceFilePath = Path.Combine(resourcesPath, resourceFilename);

    using (var writer = new ResXResourceWriter(resourceFilePath))
    {
        foreach (var picture in picturesByBitmapCollection)
        {
            writer.AddResource(picture.PictureName, new ResXFileRef(picture, typeof(Bitmap).AssemblyQualifiedName));
        }
    }
}

// Add the .resx file to the project and set the CustomTool property.
foreach (var resourceFile in Directory.GetFiles(resourcesPath, "*.resx"))
{
    var createdItem = resourcesFolder.Collection.AddFromFile(resourceFile);
    var allProperties = createdItem.Properties.Cast<EnvDTE.Property>().ToList();
    createdItem.Properties.Item("CustomTool").Value = "ResXFileCodeGenerator";
}

I have flattened the above code a little bit, cause in my real solution i use a custom class for each picture instead of the simple filename to also support the same filename in different sub folders (by using a part of the folder structure for the namespace generation). But for a first shot the above should help you.


Right click on the Resources.resx and select "Run Custom Tool".


Open the resx file and on its toolbar there's an Access Modifier menu. Set this to Public. This will generate a *.Designer.cs file.

enter image description here