Click here to Skip to main content
6,631,404 members and growing! (17,086 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » Utilities     Intermediate

Create String Variable from Text File Source

By Michael McKechney

A utility to consume a text file and produce a code snippet concatenating the contents in a string variable.. and more!
C#, VB.NET 1.1, Win2K, WinXP, Win2003VS.NET2003, Dev
Posted:2 Jun 2004
Views:59,745
Bookmarked:17 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 4.41 Rating: 4.24 out of 5
2 votes, 18.2%
1
1 vote, 9.1%
2

3
2 votes, 18.2%
4
6 votes, 54.5%
5

Sample Image - FullScreenShot_sized.jpg

Introduction

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.

License

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


Member

Occupation: Web Developer
Location: United States United States

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralDon't use StringBuilder PinmemberSteinar Herland5:25 9 Jun '04  
GeneralRe: Don't use StringBuilder PinmemberMichael McKechney5:10 10 Jun '04  
GeneralRe: Don't use StringBuilder PinmemberSteinar Herland22:20 10 Jun '04  
GeneralRe: Don't use StringBuilder PinmemberJörgen Sigvardsson12:56 1 Aug '05  
General5! PinsupporterMarc Clifton4:22 4 Jun '04  
GeneralRe: 5! PinmemberMichael McKechney8:13 4 Jun '04  
GeneralRe: 5! PinsupporterMarc Clifton7:01 8 Jun '04  
GeneralRe: 5! PinmemberFrank Hileman12:11 8 Jun '04  
GeneralRe: 5! PinmemberMichael McKechney16:19 8 Jun '04  
GeneralRe: 5! PinmemberFrank Hileman16:31 8 Jun '04  
GeneralRe: 5! PinmemberDaniel Turini12:02 14 Oct '04  
GeneralRe: 5! PinmemberMichael McKechney11:00 15 Oct '04  
GeneralLeave it as an external file PinsitebuilderUwe Keim21:32 3 Jun '04  
GeneralRe: Leave it as an external file PinmemberMichael McKechney3:27 4 Jun '04  
GeneralRe: Leave it as an external file Pinmembermav.northwind7:43 4 Jun '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Jun 2004
Editor: Smitha Vijayan
Copyright 2004 by Michael McKechney
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project