Script to append a new method to a XAML code-behind file

My company is converting a VB.NET project to a C# application and I was tasked with writing a script to automate part of the process. In particular, given information about a control name and event in VB.NET, I'm trying to insert the corresponding event into the new XAML file and its code-behind.

My approach is based partially on this Stack Overflow answer.

Basically, I'm trying to insert some text before the last two lines of a file. Specifically, I'm inserting methods into a XAML code-behind file.

The XAML code-behind is pretty typical, but just for reference, here's an example:

using System.Windows;

namespace MyNamespace
{
/// <summary>
/// Interaction logic for myWindow.xaml
/// </summary>
public partial class myWindow : Window
{
public myWindow()
{
InitializeComponent();
}

private void DO_CANCEL_Click(object sender, RoutedEventArgs e)
{
this.Close();
}

// I want to insert the new method here.
// It's possible that I'll end up inserting several, but I don't know
// how many I'll end up inserting "in advance."
}
}


I do the following:

private static void AddEvent(MethodNameParts parts)
{
// xamlFile is the full name and path of the .xaml file (the actual XML front-end)
string windowsCsFile = xamlFile + ".cs";

// Write the entire file except for the last two closing }
// I have logic (not shown) to make sure that the file is "trimmed" properly before I run this
// Thus, the last two lines will definitely be the "}" that closes the
// class and the "}" that closes the namespace
File.WriteAllLines(windowsCsFile, lines.Take(lines.Length - 2));

// parts.CSFullLine is a string containing the C# method I want to insert
File.AppendAllLines(windowsCsFile, new[] { parts.CSFullLine });
File.AppendAllLines(windowsCsFile, new[] { "}", "}" });
}


Is this a good solution, or should I do something else to avoid having to do so many writes?

• 1. What code do you want to "inject" in the file? Actually, I can't imagine a use case for that ;). 2. Do you know T4 Templates? May be a (more robust) alternative for you :). – JanDotNet Jun 28 '17 at 9:53
• @JanDotNet I'm just appending empty event handlers. – EJoshuaS Jun 28 '17 at 11:47
• I'm inserting methods into a XAML code-behind file. - Why? This sounds like a bad idea. – t3chb0t Jun 28 '17 at 17:26
• @t3chb0t I edited to mention why I'm trying to do that in the question; in retrospect, the way I originally phrased my question did sound kind of like an instance of the XY problem. – EJoshuaS Jun 29 '17 at 21:20
• I would warn against doing this. You can easily do Winforms in WPF and doing this will result in exactly that. It may result in a faster conversion, but you'll lose all the benefits of moving to WPF. You'll be better off in the long run to take advantage of WPF's MVVM architecture, but that will require a rewrite. That aside, I'll second investigating T4 templates. I've not used them myself, but seems fit for purpose. – RubberDuck Jun 30 '17 at 23:21