c# - Using File.ReadAllLines from embedded text file -


i have been applying have learned far in bob tabors absolute beginners series , wrote small console word game daughter requires me generate random 5 letter word.

i using file.readalllines(path) generate string array text file (wordlist.txt) on system , random.next generate index pull array.

i learned posts here how embed file resource unable find syntax point (path). or have access differently embedded?

thanks in advance

without a good, minimal, complete code example impossible offer specific advice.

however, basic issue this: when embed file resource, no longer file. is, original file still exists, resource not file in way. stored specific kind of data in assembly; resources embedded file sources wind binary data objects.

how use data depends on mean "embed". there 2 common ways store resources in c# program: can use "resources" object in project, exposes resource via project's ...properties.resources class (which in turn uses resourcemanager class in .net). or can add file project itself, , select "embedded resource" build option.

if using "resources" designer, there couple of different ways might have added file. 1 use "new text file..." option, allows copy/paste or type new text resource. exposed in code string property on properties.resources object. same thing happen if add resource using "existing file..." option , select file visual studio recognizes text file.

otherwise, file included byte[] object exposed property in properties.resources class.

if have used "embedded resource" build option instead of "resources" designer, data available calling assembly.getmanifestresourcestream(string) method, returns stream object. can wrapped in streamreader allow read line-by-line.

direct replacements file.readalllines(string) approach following…

using "embedded resource":

string[] readallresourcelines(string resourcename) {     using (stream stream = assembly.getentryassembly()         .getmanifestresourcestream(resourcename))     using (streamreader reader = new streamreader(stream))     {         return enumeratelines(reader).toarray();     } }  ienumerable<string> enumeratelines(textreader reader) {     string line;      while ((line = reader.readline()) != null)     {         yield return line;     } } 

using properties.resources:

you can similar when using properties.resources class. looks identical:

string[] readallresourcelines(string resourcetext) {     using (stringreader reader = new stringreader(resourcetext))     {         return enumeratelines(reader).toarray();     } } 

called string[] alllines = readallresourcelines(properties.resources.mytextfile);, mytextfile property name resource added in designer (i.e. string pass in second example text of file itself, not name of resource).

if added existing file visual studio didn't recognize text file, property type byte[] instead of string , you'll need yet different approach:

string[] readallresourcelines(byte[] resourcedata) {     using (stream stream = new memorystream(resourcedata))     using (streamreader reader = new streamreader(stream))     {         return enumeratelines(reader).toarray();     } } 

note in 3 examples, key data winds wrapped in textreader implementation, used read each line individually, populate array. these use same enumeratelines() helper method show above.

of course, see how data can retrieved, can adapt use data in variety of other ways, in case example don't want or need text represented array of string objects.