-2

Getting the files endiing with .txt is no problem in the executing assembly:

var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.txt").OrderBy(x => x).ToList();

However, I would like to get the above embedded files ending with txt from another/not executing assembly. Is this possible?

cs0815
  • 16,751
  • 45
  • 136
  • 299
  • You could just provide a library function that returns the string you are interested in. – poke Jan 19 '15 at 21:52
  • have you looked into `Assembly.GetAssembly` or how to load assembly to do that if I am understanding you correctly – MethodMan Jan 19 '15 at 21:52
  • This is not a duplicate. The referred link talks about the executing assembly. – cs0815 Jan 19 '15 at 22:01
  • I am aware of Assembly.GetAssembly but how do I get the above file names? – cs0815 Jan 19 '15 at 22:02
  • NO it has not yet been answered! The quoted link is irrelvant in thjis context!!! – cs0815 Jan 19 '15 at 22:03
  • @poke - are seriously suggesting to hard code everything!? – cs0815 Jan 19 '15 at 22:05
  • Really wish people who down-vote, close etc. would spend some time to understand the problem – cs0815 Jan 19 '15 at 22:06
  • Well, actually it's not easy to understand your question, because connection between searching by mask in your sample and embedded file ending isn't clear. And if assembly isn't executing, why you can't load it and get resource from it? – Sergey Litvinov Jan 19 '15 at 22:10
  • I tested the above code. It retrieves all files with this extension from the executing assembly. However, I would like to extract them from another/not executing assembly. Sorry if this was not clear. – cs0815 Jan 19 '15 at 22:13
  • Your code retrieves these files not from the assembly, but from the folder where assembly is located. Retrieving files from assembly means getting embedded files from the assembly itself by using `assembly.GetManifestResourceStream("SomeName.txt")` methods. Do you know the path to another assembly? Is it installed somewhere? how do you know what assembly do you need? – Sergey Litvinov Jan 19 '15 at 22:18
  • Have a look at my old question: http://stackoverflow.com/questions/17569931/get-embedded-resource-in-another-project Problem - I do not want to hardcode the file names – cs0815 Jan 19 '15 at 22:22
  • Anyway you need some way to identify that assembly. You can install it to GAC and find it `AssemblyQualifiedName` like `System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089`. This approach also will search in current folder. Or you can save path to some xml config\settings file but it also will be some kind of hardcode. – Sergey Litvinov Jan 19 '15 at 22:27
  • 1
    @csetzkorn Embedding a text file into an assembly is the equivalent of hardcoding a string. – poke Jan 19 '15 at 22:32
  • @poke hardcoded only in the sense that the files end with txt. That's it. Can always add more files and change the content (I.e. data), which is sourcecontrolled. So I tend to disaggree - sorry. – cs0815 Jan 19 '15 at 22:50
  • If you downvote provide at least a reason. – cs0815 Jan 19 '15 at 22:57
  • @csetzkorn If it’s **built** into the assembly, it’s hardcoded. Doesn’t matter where it comes from. – poke Jan 19 '15 at 22:59
  • @poke yes I know. However, I can still add more txt files and then loop over them ... that's the requirement! – cs0815 Jan 20 '15 at 08:53
  • If you embed them *in the assembly* you cannot without recompiling the assembly. What you are doing in your code has nothing to do with embedding things anywhere. – poke Jan 20 '15 at 08:57

1 Answers1

1

The above is not grabbing an "embedded" file, it's grabbing all .txt files located in the same directory as the running application.

If you're interested in the directory of another Assembly, you need to get that other Assembly. This can be done if you have a reference to the Assembly and know a public class (ex: SomeType) within the Assembly:

var path = typeof(SomeType).Assembly.Location;
var folder = new FileInfo(path).DirectoryName;
var files = Directory.GetFiles(folder, "*.txt").OrderBy(x => x).ToList();

Depending on the assembly, there might also be some data on your system, such as an environment variable or a GAC Entry.

If you don't have the assembly referenced or know that the file is keeping special flags in the system, you'll have to have some other way to get it, which becomes little different than simply knowing the path by putting it in a configuration file, database, or similar resource.

David
  • 10,458
  • 1
  • 28
  • 40
  • I know this. That's not the point. I do not know the directory as it is in another assembly! – cs0815 Jan 19 '15 at 22:04
  • If it's part of another assembly, your program has to reference it somehow. I'll update my answer – David Jan 19 '15 at 22:22
  • Yeah, you can get to it using var assembly = typeof(BlaClass).Assembly; where BlaClass is from the assembly containing the files ... not sure how to get all files ending with txt though! – cs0815 Jan 19 '15 at 22:27