Click here to Skip to main content
12,763,515 members (32,687 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 1 Dec 2004

File2SB - StringBuilder generator

, 1 Dec 2004
Rate this:
Please Sign up or sign in to vote.
File2SB creates StringBuilder functions from text files.

Sample Image - File2SB.jpg


Template generation is all the rage these days, especially amongst developers who frequently code similar blocks and functions with only minor differences between them. There are lots of good template based programs out there, but sometimes you need to code something specific for your own internal process.

The Basics of Coding for Templates

Template based code generation goes something like this - first, you create a template. A typical template is a text file, with the fields you need to replace marked in some special way. For example:

Private Function <functionname> As String
  Return "This is Function <functionname>!"
End Function

OK, yes - it is admittedly a stupid example. Anyway, you would typically read the template into a string variable, and then perform a simple Replace() as required, like this:

Dim MyTemplate As String = GetTemplate(filename)
' GetTemplate is some function you wrote that reads and returns the template
Dim FinishedProduct as String = MyTemplate.Replace("<functionname>","HelloWorld")

which would return:

Private Function HelloWorld As String
  Return "This is Function HelloWorld!"
End Function

The Problems with Templates

There are two ways of "getting" templates. The first is read in a text file from somewhere that has already had the "fields" added by hand. This is the most common method of dealing with templates, as the end user can simply edit the templates with any given text editor if they need to change them.

The problem is, sometimes you don't want to allow the end user to change the templates. For example, you might have a program that generates legal forms or corporate documents, that need to remain unchanged except for the fields you designate. In such a case, reading in the text files would be bad, as a malicious end user could alter the template and render the results invalid or unwanted. Or, you wish to offer the option to regenerate the templates to their original status as a program feature.

The answer to this is to use the second method of "getting" templates - generating them internally from code. Using the above example, this would look something like this:

Imports System.Text
Function GetTemplate() As String
  Dim sb As New StringBuilder
  sb.Append ("Private Function <functionname> As String")
  sb.Append ("Return ""This is Function <functionname>!""")
  sb.Append ("End Function")
  Return sb.ToString()
End Function

Now, you can guarantee that the template you want will be exactly what you intended it to be.

The Problem Expands

Let's take this one step further. Let's say that your program needs to generate not just one legal document, but hundreds, and they are lengthy technical documents. Writing all the sb.Append() statements to accomplish this would take weeks or even months. Plus, the process can be made more difficult when encountering things such as double quotes - see the special notation that was required above?

The Solution

This is where File2SB steps in. File2SB will read in a given text document, and convert that document into a Function that returns the template as a string. It writes all the required lines for the StringBuilder, and corrects for things like the double quotes. File2SB can write the function in either VB.NET or C# notation as well. Simply click File | Open to read in the template and convert it. From there, you can either cut and paste the converted file into your application, or save it to a text file. At that point, a simple Search and Replace can usually assist you in adding "fields" to your templates.

If you would like to see how File2SB was used to create another tool, please see my other project, PACT.


I'm a VB.NET developer by trade, so the C# coding is really untested. If you have any problems with it, please let me know so that I can fix the problem and re-release the code. I hope you enjoy File2SB.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Todd Davis
Systems Engineer Virtual RadioLogic
United States United States
Todd Davis has been working in web and application development for several years, using Silverlight, ASP.NET, VB.NET, C#, C++ and Javascript, as well as a great deal of work with SQL server and IIS.

He currently works for Virtual Radiologic in Eden Prairie, MN, however he is better known for his varied work in the open source community, especially the DotNetNuke project for which he provided several world-renowned training videos and modules. A huge advocate of open source and open knowledge sharing, everything on his website ( is always offered for free.

Whenever he is not actively coding at his laptop (a rarity to be sure), he can be found woodworking, walking with his wife and kids, or motoring along the back roads of MN on his Harley Davidson Fatboy.

You may also be interested in...

Comments and Discussions

QuestionWhy not include as resource ? Pin
D4Skunk1-Dec-04 21:09
memberD4Skunk1-Dec-04 21:09 
AnswerRe: Why not include as resource ? Pin
Todd Davis2-Dec-04 3:44
memberTodd Davis2-Dec-04 3:44 
GeneralRe: Why not include as resource ? Pin
D4Skunk2-Dec-04 3:56
memberD4Skunk2-Dec-04 3:56 
GeneralRe: Why not include as resource ? Pin
Todd Davis2-Dec-04 7:30
memberTodd Davis2-Dec-04 7:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 1 Dec 2004
Article Copyright 2004 by Todd Davis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid