In C #, is there a way to get a link to a string path to an embedded resource file?

I have a method that takes a string parameter, which is the file path to the text file. I want to be able to transfer a text file that I embedded as a resource in my assembly.

Is there a way to get a lowercase link to an embedded text file so that it functions as a file path to open StreamReader?

Thanks.

+4
source share
3 answers

You can use Assembly.GetManifestResourceStream(resource_name_of_the_file) to access the file stream, write it to the TEMP directory, and use this path.

For example, if you have a file in your project on the path " Resources \ Files \ .txt File " and the default namespace for the project assembly is RootNamespace ", you can access the file stream from this assembly code using

 Assembly.GetExecutingAssembly().GetManifestResourceStream("RootNamespace.Resources.Files.File.txt") 
+6
source

Is there a way to get a lowercase link to an embedded text file so that it functions as a file path to open StreamReader?

No, the embedded resource is not a separate file, but embedded in the executable file. However, you can get a stream that you can read using StreamReader .

 var name = "..."; using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name)) using (var streamReader = new StreamReader(stream)) { // Read the embedded file ... } 
+5
source

So, the full code will look like this:

  var f = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames(); var tempPath = Path.GetTempPath(); foreach (var c in f) { using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(c)) { try { var p = tempPath + c.Replace("Ner.ner.", ""); using (FileStream fs = File.OpenWrite(p)) { stream.CopyTo(fs); } } catch { } } } var classifier = CRFClassifier.getClassifierNoExceptions( tempPath + @"english.muc.7class.distsim.crf.ser.gz"); 
0
source

All Articles