[英]How to use a resource file in Visual Studio 2015
I'm creating integration tests (web class library project) for my ASP5 MVC6 application. 我正在为我的ASP5 MVC6应用程序创建集成测试(Web类库项目) 。 I want to add a resource file with raw SQL to the test project for DB preparation.
我想将一个带有原始SQL的资源文件添加到测试项目中以进行数据库准备。
I wanted to use good old resources (.resx). 我想使用好的旧资源(.resx)。 But I it's not available in
add new item
project's menu. 但我没有
add new item
项目的菜单。
I found this answer pointing to a github repo . 我发现这个答案指向一个github回购 。 I guess this is where he reads the file:
我猜这是他读取文件的地方:
using (var stream = FileProvider.GetFileInfo("compiler/resources/layout.html").CreateReadStream())
using (var streamReader = new StreamReader(stream))
{
return streamReader.ReadToEnd().Replace("~", $"{basePath}/compiler/resources");
}
I tried using System.IO.File.ReadAllText("compiler/resources/my.sql")
in my test helper project in TestHelper class. 我尝试在TestHelper类的test helper项目中使用
System.IO.File.ReadAllText("compiler/resources/my.sql")
。
When I used the TestHelper class in my actual test project it was looking for the the file in test project directory. 当我在我的实际测试项目中使用TestHelper类时,它正在测试项目目录中查找该文件。
TestProject/compiler/resources/my.sql
insetad of TestHelperProject/compiler/resources/my.sql
TestProject/compiler/resources/my.sql
insetad of TestHelperProject/compiler/resources/my.sql
I can figure out a couple of workarounds. 我可以找出几个解决方法。 However I'd like to do it the right way.
但是我想以正确的方式做到这一点。 Preferably like I would do in with a resx file:
最好像我用resx文件一样:
string sql = Resources.MySql;
Any suggestions? 有什么建议么?
EDIT 编辑
It is a little more complicated because there's no tooling support but it is doable: 它有点复杂,因为没有工具支持,但它是可行的:
resx
file or copy it from somewhere else like a classic C# project resx
文件或从其他地方复制它,如经典的C#项目 sake
as the build system, then you can use the same target that we, the ASP.NET team use: https://github.com/aspnet/Universe/blob/dev/build/_k-generate-resx.shade otherwise, you need to implement something similar in your own build scripts sake
作为构建系统,那么您可以使用我们ASP.NET团队使用的相同目标: https : //github.com/aspnet/Universe/blob/dev/build/_k-generate-resx .shade否则,你需要在自己的构建脚本中实现类似的东西 compiler/resources
. compiler/resources
。 If you place them under any other folder then include them in the project json (like here )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.