0

I have a dll file as a resource in my project. Now I would like to access the directory folder in the dll. E.g. Image dll (In Image.dll -> \Image\PresetFolder)

I would like to Directory.Getdirectories() folder path in the Image.dll

How could I can achieve this in c#???

Ling
  • 87
  • 1
  • 13
  • You mean `\Image\PresetFolder` is already backed in DLL and you want to access it? How can a DLL have folder inside it? – Chetan Mar 04 '21 at 09:15
  • Yes, this makes little sense "*Now I would like to access the directory folder in the dll*" its like asking how to find the smell of yellow when in a square circle... Maybe you could clarify what it is you want to access a little more – TheGeneral Mar 04 '21 at 09:16
  • okay, I have a proj file (A proj), this proj file have a Image\PresetFolder, and I make this as a dll and use it in another proj (B proj). And now I wan access folder that I create in A proj (dll) from B proj. Something like this. – Ling Mar 04 '21 at 09:23
  • Not sure what you are eventually trying to achieve but you can expose a method from A.dll which will return the filelist or contents for the folder. So B proj just need to call the method without worrying about the actual path... – Chetan Mar 04 '21 at 09:25
  • So, when you create an assembly, it doesnt matter what you put in the folders of the project, even if its set to copy to output directory... unless its code it doesn't make it into the assembly. The exception to this is if you embed the files in a resource – TheGeneral Mar 04 '21 at 09:26
  • There are no folders in dll. Do you have resources there which you want to access from exe? [Related](https://stackoverflow.com/q/2771130/1997232). – Sinatr Mar 04 '21 at 09:44
  • I have try Chetan work around method, but GetDirectory need a full path to access the folder, and when I using Application.GetCurrentDirectory (or others method) in A proj, thn run the application for B proj, the base path always point to B proj path, this cause it cannot get a correct path. – Ling Mar 04 '21 at 10:00
  • @Sinatr, ya I need to loop through the resource file in a folder to display all the resource file, that why I need to get the directory folder. – Ling Mar 04 '21 at 10:05
  • Does this answer your question? [Get a list of all resources from an assembly](https://stackoverflow.com/questions/52179108/get-a-list-of-all-resources-from-an-assembly) – Sinatr Mar 04 '21 at 10:27
  • No, my dll resource is include by build action as 'Resource' and just include in my main proj file. So it's no point to loop through resx. – Ling Mar 05 '21 at 06:24

1 Answers1

0

At last, my friend provide me the answer, you have to load the dll first before get the directory path with code

private string[] GetAllResourcePath()
{
    Assembly assembly = Assembly.Load("ImageDLL");
    string resName = "ImageDLL.g.resources";
        using (var stream = assembly.GetManifestResourceStream(resName))
        {
            using(var reader = new ResourceReader(stream))
            {
                return reader.Cast<DictionaryEntry>().Select(x => (string)x.Key).ToArray();
            }
        }
}

From this func, it will return all the resource directory path, and you can just filter out by using .Where() linq to get the directory you want.

Ling
  • 87
  • 1
  • 13