简体   繁体   English

在执行项目C#中获取文件的路径

[英]Get path to File in executing project c#

I have two separate projects in Solution. 我在解决方案中有两个单独的项目。

First project is just a Test Console project. 第一个项目只是一个测试控制台项目。 And the second project is Common Lib project. 第二个项目是Common Lib项目。

I have static class in Common Lib project that contains a static method which requires reading the file (for OpenNLP). 我在Common Lib项目中有一个静态类,其中包含一个静态方法,该方法需要读取文件(对于OpenNLP)。

var sentenceDetector = new EnglishMaximumEntropySentenceDetector("EnglishSD.nbin");

I added file "EnglishSD.nbin" into Common Lib project. 我将文件“ EnglishSD.nbin”添加到Common Lib项目中。 I changed the property of that file to ' Content ' and ' Copy always ' too. 我也将该文件的属性更改为“ 内容 ”和“ 始终复制 ”。 It does add that file to bin/debug folder of Common Lib project. 确实将该文件添加到Common Lib项目的bin / debug文件夹中。 EDIT: 'EnglishSD.nbin' is located in folder OpenNLP/EnglishSD.nbin 编辑: “ EnglishSD.nbin”位于文件夹OpenNLP / EnglishSD.nbin中

Issue: When I run test console project which uses the static function in Common lib project. 问题:当我运行使用Common lib项目中的静态功能的测试控制台项目时。 It tries to look for 'EnglishSD.nbin' into the bin folder of Test Console Project instead of Common Lib project. 它尝试在Test Console Project的bin文件夹而不是Common Lib项目中查找“ EnglishSD.nbin”。 Which leads to an error of 'FileNotFound'. 导致错误“ FileNotFound”。

Is there any way to get the path of the file without worrying about where it's being called from? 有什么方法可以获取文件的路径,而不必担心从何处调用文件吗?

So your Test Console references your Common Lib ? 因此,您的测试控制台引用了您的Common Lib吗? Then, when you build the Test Console , the CommonLib.dll will be copied to the Test Console project folder. 然后,当您构建测试控制台时CommonLib.dll将被复制到测试控制台项目文件夹中。 I don't know if there is an automatic way of copying dependencies of libraries that are referenced by the main project... 我不知道是否存在一种自动方法来复制主项目引用的库的依赖关系...

But you can setup that. 但是您可以进行设置。 Use the post-build-event of the Test Console project and copy your required file manually (I assume that your Common Lib project folder is /Common Lib ): 使用测试控制台项目的构建后事件并手动复制所需的文件(我假设您的Common Lib项目文件夹为/Common Lib ):
copy "$(SolutionDir)Common Lib\\EnglishSD.nbin" "$(TargetDir)"

My bad guys. 我的坏家伙。 I actually had 'EnglishSD.nbin' in the folder. 我实际上在文件夹中有“ EnglishSD.nbin”。 Therefore it created a folder in TestConsole projects bin/debug and placed files there. 因此,它在TestConsole项目bin / debug中创建了一个文件夹,并将文件放置在该文件夹中。

So I just needed to change the path to (FolderName)/EnglishSD.nbin. 所以我只需要将路径更改为(FolderName)/EnglishSD.nbin。

 var sentenceDetector = new EnglishMaximumEntropySentenceDetector("OpenNLP/EnglishSD.nbin");

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

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