简体   繁体   中英

How to use a resource file in Visual Studio 2015

I'm creating integration tests (web class library project) for my ASP5 MVC6 application. I want to add a resource file with raw SQL to the test project for DB preparation.

I wanted to use good old resources (.resx). But I it's not available in add new item project's menu.

I found this answer pointing to a github repo . 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.

When I used the TestHelper class in my actual test project it was looking for the the file in test project directory.

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:

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
  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
  3. Place the resources either compiler/resources . If you place them under any other folder then include them in the project json (like here )
  4. Build using sake then build using dnx. Your resource should be available as a strong type object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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