3

I'm building a class library which use a .dat file, inside the library i made a folder App_Data and put the .dat file inside it, how can i have my library to use the .dat file. How could it be included at the dll when build?

I already tried Path.GetFullPath, and almost anything in Path, but all i get is the path to debug folder, and in that folder i dont even have the .dat file which i expect to be included because the .dat file is in the project.

I just want to use that .dat file without hard coding its path, and it should still work when referenced.

Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
sclang
  • 229
  • 2
  • 15

1 Answers1

5

You must right-click the .dat file and set Copy to Output Directory = Copy Always. Doing so, the path to the debug folder will correctly point to the file. No need to hard code the path.

enter image description here

If you're interested in embedding the .dat file in the DLL (a bit more work), you can go this way:

Can I embed other files in a DLL?

Community
  • 1
  • 1
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • I see that the entry at your build action is Compile, in mine it's None, should i change it to Compile or leave it that way? – sclang May 22 '13 at 02:52
  • No need to set `Build Action`... the picture is just to show the property. :) – Leniel Maccaferri May 22 '13 at 02:53
  • Can I also ask how can i reference my class library to other solutions?, What I do is I open the library to my solution and add it to the reference, Is there a better way like copying the DLL, and not the library itself? Since you mention embedding it in the DLL, I would like to do it, but how can i reference just using the DLL and there are two DLLs that are build one in debug and the other at obj folder, which one should i use? – sclang May 22 '13 at 02:59
  • 1
    The correct way is adding the class library project as a reference in any other project that depends on the class library. Manually copying the `DLL` is not recommended. – Leniel Maccaferri May 22 '13 at 03:01
  • And one last thing, is it fine for me to use any of the following Path.GetFullPath or HostingEnvironment.MapPath, or which one should i use? or are there better ways to locate it? thanks – sclang May 22 '13 at 03:07
  • 1
    If `Path.GetFullPath` resolves to the `.dat` correctly, stick to it. – Leniel Maccaferri May 22 '13 at 03:08