Jeff Stock
Jeff Stock

Reputation: 3856

Read text file within a .NET project

Is there a way I can open and read a text file within a .NET project? I know I can use the System.IO.StreamReader class to do it for a Windows text file.

I'd like to store some SQL scripts for upgrading my database in text files within my project. Since I'm writing this in VB I can't just paste the scripts directly into my code due to line continuations, where I could if it were C#. These are very long scripts.

Upvotes: 2

Views: 1517

Answers (4)

Secura
Secura

Reputation: 1

I ran across this problem myself. I can see that Jeff accepted the solution where you copy out the sourcefiles into your project folder.

I however am not comfortable having db-create/drop/alter scripts lying around on fileareas. I found this question was asked again later on. Here is what solution I implemented using imbedded resources to great success.

Our solution involves distributed databases spread over possibly several database instances. We implemented a solution that includes a master database containing all setupinformation of the 'shard'-databases. Having a 'SqlUpdate'-engine we can keep all the distributed shards up to date.

Upvotes: 0

Lucero
Lucero

Reputation: 60190

You can use embedded resources and use a StreamReader on the stream returned by Assembly.GetManifestResourceStream to read those.

Upvotes: 0

Richard
Richard

Reputation: 108975

Embed as resources in the assembly, then use ResourceManager to read them into a Stream or String at runtime?

Upvotes: 1

Andomar
Andomar

Reputation: 238078

Text files inside a project are not added to your executable. You can enable Copy to Output Directory in the properties for the .sql file. This will copy them alongside your assemblies to the bin directory.

From there, your program could read them like any other file.

Upvotes: 5

Related Questions