Click here to Skip to main content
11,928,352 members (51,916 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

Create String Variable from Text File Source

, 2 Jun 2004
Rate this:
Please Sign up or sign in to vote.
A utility to consume a text file and produce a code snippet concatenating the contents in a string variable.. and more!

Sample Image - FullScreenShot_sized.jpg


Sometimes it's necessary to create large string variables in code. One recent example I was working on was a project whose database component was "self healing" (i.e., if any of the necessary tables were missing, the application would automatically recreate them). Creating and concatenating these large strings can be dull, cumbersome, and is prone to error. Since the table creation scripts could easily be saved to a file, I wrote this utility to process the text file into a code string. I've also used it for HTML snippets and JavaScript code for web controls.

Using the Utility

After selecting a source file to use, you can select a code format output, C#, VB.NET and VB6.

There are several other output options, such as preserving white space and blank lines.

To make the generated code more useful, you can add text before and after the source file. I commonly use this to define method signatures and return values respectively. You can also add text to the string variable before and after the source file. Once you've defined how you want the file processed, just run the utility to see the output. You can then right-click on the output textbox and either copy the results to the clipboard or save it to a file.

I've tried to add some convenient items to the utility, such as, the default file extension for saving the result matches the selected code output (i.e., ".cs" for C#, etc.). Also, each label has a tool tip to help explain the usage of the item.

The code that runs this application is fairly simple, processing the basic defined rules and interacting through the lines in the file. I've attempted to make the code as modular as possible if you wanted to add another language output.

I'd love to hear any suggestions on making this utility even more useful, but until then, I hope you find this as handy as I have.


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

Michael McKechney
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralDon't use StringBuilder Pin
Steinar Herland9-Jun-04 5:25
memberSteinar Herland9-Jun-04 5:25 
GeneralRe: Don't use StringBuilder Pin
Michael McKechney10-Jun-04 5:10
memberMichael McKechney10-Jun-04 5:10 
GeneralRe: Don't use StringBuilder Pin
Steinar Herland10-Jun-04 22:20
memberSteinar Herland10-Jun-04 22:20 
GeneralRe: Don't use StringBuilder Pin
Jörgen Sigvardsson1-Aug-05 12:56
memberJörgen Sigvardsson1-Aug-05 12:56 
General5! Pin
Marc Clifton4-Jun-04 4:22
protectorMarc Clifton4-Jun-04 4:22 
GeneralRe: 5! Pin
Michael McKechney4-Jun-04 8:13
memberMichael McKechney4-Jun-04 8:13 
GeneralRe: 5! Pin
Marc Clifton8-Jun-04 7:01
protectorMarc Clifton8-Jun-04 7:01 
GeneralRe: 5! Pin
Frank Hileman8-Jun-04 12:11
memberFrank Hileman8-Jun-04 12:11 
GeneralRe: 5! Pin
Michael McKechney8-Jun-04 16:19
memberMichael McKechney8-Jun-04 16:19 
GeneralRe: 5! Pin
Frank Hileman8-Jun-04 16:31
memberFrank Hileman8-Jun-04 16:31 
GeneralRe: 5! Pin
Daniel Turini14-Oct-04 12:02
memberDaniel Turini14-Oct-04 12:02 
GeneralRe: 5! Pin
Michael McKechney15-Oct-04 11:00
memberMichael McKechney15-Oct-04 11:00 
GeneralLeave it as an external file Pin
Uwe Keim3-Jun-04 21:32
sitebuilderUwe Keim3-Jun-04 21:32 
GeneralRe: Leave it as an external file Pin
Michael McKechney4-Jun-04 3:27
memberMichael McKechney4-Jun-04 3:27 
I agree, in my example, by encapsulating the SQL statements in the code, you do lose flexibility and would need to recompile to make simple SQL changes. In the project I alluded to, a requirement was "single file deployment" (i.e. they wanted one .exe file with no configuration files, etc.) This was the solution they wanted.
Also, be leaving the SQL statements externally, you open your statements to “hacking”, either malicious or accidental, that could break your app.
GeneralRe: Leave it as an external file Pin
mav.northwind4-Jun-04 7:43
membermav.northwind4-Jun-04 7:43 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 3 Jun 2004
Article Copyright 2004 by Michael McKechney
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid