简体   繁体   English

c# class 库使用文件

[英]c# class library use files

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.我正在构建一个使用.dat文件的 class 库,在库中我创建了一个文件夹App_Data并将.dat文件放入其中,我怎样才能让我的库使用.dat文件。 How could it be included at the dll when build?构建时如何将其包含在 dll 中?

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.我已经尝试过Path.GetFullPath和 Path 中的几乎所有内容,但我得到的只是调试文件夹的路径,在该文件夹中我什至没有我希望包含的.dat文件,因为.dat文件位于项目。

I just want to use that .dat file without hard coding its path, and it should still work when referenced.我只想使用那个.dat文件而不对其路径进行硬编码,并且在引用时它应该仍然有效。

You must right-click the .dat file and set Copy to Output Directory = Copy Always . 您必须右键单击.dat文件并将“ 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. 无需硬编码路径。

在此输入图像描述

If you're interested in embedding the .dat file in the DLL (a bit more work), you can go this way: 如果您对将.dat文件嵌入DLL感兴趣(稍微多一些工作),可以这样做:

Can I embed other files in a DLL? 我可以在DLL中嵌入其他文件吗?

  1. right-click the file and select Properties右键单击文件和 select 属性
  2. set Copy to Output Directory = Copy Always将复制设置为 Output 目录 = 始终复制
  3. not change other properties (for example Build Action, ...)不更改其他属性(例如 Build Action,...)

将文件添加到输出文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM