简体   繁体   English

如何在Visual Studio 2015中使用资源文件

[英]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 编辑

Project type 项目类型 在此输入图像描述

Available items 可用物品 在此输入图像描述

It is a little more complicated because there's no tooling support but it is doable: 它有点复杂,因为没有工具支持,但它是可行的:

  1. Create a resx file or copy it from somewhere else like a classic C# project 创建一个resx文件或从其他地方复制它,如经典的C#项目
  2. If you can use 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否则,你需要在自己的构建脚本中实现类似的东西
  3. Place the resources either compiler/resources . 将资源放置在compiler/resources If you place them under any other folder then include them in the project json (like here ) 如果将它们放在任何其他文件夹下,则将它们包含在项目json中(如此
  4. Build using sake then build using dnx. 使用清酒构建然后使用dnx构建。 Your resource should be available as a strong type object 您的资源应该可用作强类型对象

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

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