[英]How to read connection string inside .NET Standard Class library project from ASP.NET Core
[英]NET Core Class Library read file in a project folder
我有一個 NET Core Class 庫項目,需要從項目中的文件夾中讀取文件內容(模板文件)。 我正在使用 Visual Studio 代碼
文件存儲如下:
--Application (root)
---Functions (library for functions)
---(c# classes)
---Services (library for services)
---(c# classes)
---Templates (templates)
-----template1.txt
-----template2.txt
-----template3.txt
我怎樣才能做到這一點? 另外,對於 Unix 和操作系統等其他操作系統是否安全?
希望我能正確理解你的問題。
你可以做類似的事情:
using System.IO;
public class MyLocations
{
public static readonly string App = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
public static readonly string Templates = Path.Combine(App, "Templates");
}
在您的Main
您可以執行以下操作:
static void Main(string[] args)
{
foreach ( var filePath in Directory.EnumerateFiles(MyLocations.Templates) )
{
var contents = File.ReadAllText(filePath);
// do something with "contents"
}
}
這應該 function 在 Mac 和 Linux 機器上正確。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.