0

Hello I have a Dll with one folder inside it, and I want to know how can I access that folder from my C# class also in my Dll.

I tried with:

  AppDomain.CurrentDomain.BaseDirectory + @"/tesseract-master.1153";

but it only gets my project folder not my dll.

I dont know if this is right, hope someone can help me, thanks.

EDIT: I need that folder so my DLL can work. What its the correct way to "attach" a folder to my DLL?

How can i access this folder

jonaChaz
  • 301
  • 3
  • 17
  • 2
    Can you explain what you mean by a DLL with a folder inside it? Does it have a string that's a path to a folder or something? DLLs don't contain folders. – itsme86 Jul 03 '19 at 14:39
  • 1
    Do you mean a folder with one DLL inside it? We tend to take things literally. If neither one of those is what you literally mean then I recommend editing the question to be more specific. – Scott Hannen Jul 03 '19 at 14:40
  • i have a folder inside my library – jonaChaz Jul 03 '19 at 14:42
  • i need that folder so my dll can work, then how is the correct way to do this? – jonaChaz Jul 03 '19 at 14:43
  • 1
    We have no idea what you mean by having a folder inside your library. More information is needed. – itsme86 Jul 03 '19 at 14:46
  • 1
    There are no folders in DLLs. There are also no cats in DLL. Unless i stuff a cat into a DLL, then there would be a cat inside a DLL. Don't try this with dogs, though... Long story short, and joking aside: It is entirely unclear and vague what you are trying to explain here. Unless you are explaining accurately and in detail what you actually mean by saying "_i have a folder inside my library_", no one can help you... –  Jul 03 '19 at 14:47
  • i uploaded an image hope it helps – jonaChaz Jul 03 '19 at 14:50
  • 1
    "References" is not a folder in a DLL. It is just a virtual project folder, inside your programming project, to list all the assembly references needed by your project. –  Jul 03 '19 at 14:50
  • i mean the folder not the references – jonaChaz Jul 03 '19 at 14:50
  • 1
    Which folder then? Tesseract-master.1153? This folder is not being put in the DLL either. This folder might contain files. Those files might perhaps be put as some kind of resources into your DLL, but if and how would entirely depend on the project settings for these files (which could be different for each of those files). –  Jul 03 '19 at 14:53
  • then how can i put that folder as resources for my dll because my dll cannot work without those files – jonaChaz Jul 03 '19 at 14:55
  • 1
    Don't put those files/folder into your DLL then. Just bundle and copy/install them alongside with your DLL that needs them. –  Jul 03 '19 at 14:57
  • I advise you to read more about dll. Good Luck – Mohammad Ghanem Jul 03 '19 at 14:57
  • 1
    From within a method inside your DLL, invoke `Assembly.GetExecutingAssembly()` to get the `Assembly` object representing the DLL calling GetExecutingAssembly (in this case: your DLL). You can then query this Assembly object to get the directory path/location of your DLL. From there it should then be easy to build an absolute path for the tesseract folder, assuming the tesseract folder is within the same folder as your DLL... –  Jul 03 '19 at 14:59
  • thanks @elgonzo that is how i do it but i thought i could embedd the folder into my dll – jonaChaz Jul 03 '19 at 15:03
  • 1
    Well, you could embed those files (in some way), but for your program to be able to work with the tesseract files (and tesseract itself being able to work with its own files), you would need to extract files from the DLL before use anyway. Would be much work/effort/sweat/blood for a rather little benefit compared to just leaving the files "outside" of your DLL... –  Jul 03 '19 at 15:06

1 Answers1

1

You don't have folders within a DLL. A .Net assembly (DLL) only contains:

  • The assembly manifest
  • Type declarations (to be used by other libraries)
  • MSIL code that implements these types (compiled code that actually runs)
  • binary/string resources

What you probably want to use is resources, i.e. embedding files from your Visual Studio project structure into the output assembly. They can and will be organized by using namespaces, which resembles folders. You can select a BuildAction Embedded Resource in the file properties for individual files to do so (select the file and R-Click > Properties). These resources can be accessed from the code using

Assembly.GetExecutingAssembly().GetManifestResourceStream("TessDll.tesseract-master.1153.your-file-here")`. 

I'm not sure about the . in your folder name, maybe you need to avoid that. If you want to embed all content of a folder as a resource, you need to edit the .csproj file as described here.

I recommend downloading ILSpy - it's a tool by which you can open .net assemblies and have a peek what's inside.

Alternatively, be aware that you don't need to embed your resources into your assembly to have them available in the bin output folder. You can just set the Copy to Output Directory property to true in the file properties and then access the file using your original approach with AppDomain.CurrentDomain.BaseDirectory. Again, you would have to do that for every single file manually, but you can also do it for a whole folder by editing the .csproj file with wildcards:

  <ItemGroup>
       <Content Include=".\tesseract-master.1153\**">
           <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Content>
  </ItemGroup>
Efrain
  • 3,248
  • 4
  • 34
  • 61