asp.net-core - 访问我的项目根目录中的文件夹 - ASP.NET Core 3.1
问题描述
我有一个名为的根文件夹TestLogging
,其中包含一个解决方案。我有一个控制台应用程序,它有自己的目录,还有一个测试项目,它也有自己的目录。
在 Windows 资源管理器中,我在根文件夹中手动创建了一个名为 Configuration 的目录TestLogging
,其中有一个xml
名为Configuration.config
.
我正在尝试通过代码访问此路径以引用此文件,而不必使用此文件的绝对路径。
我已经尝试过使用here的各种示例System.IO
,它们都指向正在运行的当前项目的构建目录,例如MyConsoleApp\bin\Debug\netcoreapp3.1
以下是我尝试过的一些事情:
string defaultPath = System.IO.Directory.GetCurrentDirectory();
// this does the same as above!
string pathToConsoleApp = Path.GetFullPath(AppContext.BaseDirectory);
// very similar to the other two!
var projectPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)));
如果已重复,请提前申请。
解决方案
解决方案
string configpPath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Configuration.config.xml");
测试代码
class Program
{
static void Main(string[] args)
{
string defaultPath = System.IO.Directory.GetCurrentDirectory();
// this does the same as above!
string pathToConsoleApp = Path.GetFullPath(AppContext.BaseDirectory);
// very similar to the other two!
var projectPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)));
string configpPath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Configuration.config.xml");
Console.WriteLine("Hello World!");
Console.WriteLine("defaultPath: " + defaultPath);
Console.WriteLine("pathToConsoleApp: " + pathToConsoleApp);
Console.WriteLine("projectPath: " + projectPath);
Console.WriteLine("configpPath: " + configpPath);
}
}
测试结果
推荐阅读
- java - 是否可以停用现有消息通道的 Spring Integration 检查?
- java - 如何在会话中设置 B2B 单元(Hybris)?
- c# - WPF DataGridHyperlinkColumn 未触发 Click 事件
- javascript - 对象键的数字顺序从 0 javascript 重新排列
- c - 当它是 sizeof() 函数的参数时,为什么 n++ 不起作用?
- celery - 我可以对 celery 任务对象进行本地回调吗?
- ios - 使用 iOS 将 Intel Realsense D435 连接到 iPhone
- c++ - 使用 std::thread 在 C++ 中的单独线程中执行每个对象
- tabs - Flutter - 如何正确删除 TabBarView 中的选项卡
- javascript - 需要一个 json 文件导致错误:找不到模块