如何在解决方案C#中获取另一个项目的绝对路径 [英] How to get absolute path of another project in a solution C#

查看:419
本文介绍了如何在解决方案C#中获取另一个项目的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个项目的解决方案.一个项目仅用于处理所有数据,而另一个项目(启动项目)则用于所有网络.

I have a solution containing two projects. One project is just for doing all data stuff and the other one, the startup project, do all the web stuff.

现在,我想通过首先获取项目的根目录从TaskManagerHelpers类中获取TasksDataBase.xml.但是我得到的只是TaskManager.Web根目录. (我从TaskManager.Web内部的控制器调用TaskManagerHelpers.cs内部的方法)

Now I want to get the TasksDataBase.xml from the TaskManagerHelpers class by first getting the projects root directory. But all I get is the TaskManager.Web root directory. (I call the method inside TaskManagerHelpers.cs from a controller inside TaskManager.Web)

当我在同一个项目中的类中时,如何获得TaskManager.Data根目录?

How do I get the TaskManager.Data root directory when I'm in a class in the same project?

我已经尝试过这些方法和类似方法.

I've tried with theese methodes and similar ones.

  • HttpContext.Current.Request.PhysicalApplicationPath;
  • System.IO.Path.GetFullPath();
  • AppDomain.CurrentDomain.BaseDirectory;

提前谢谢!

推荐答案

一种可能性是 embed the XML file into the assembly ,然后将其作为Web应用程序中的资源读取.请记住,当您将Web应用程序发布到Web服务器时,进入软件包的所有内容都是该Web应用程序的文件.与该Web应用程序所在的Visual Studio解决方案中可能存在的某些项目没有物理关系.

One possibility is to embed the XML file into the assembly of the class library and then read it as resource in your web application. Remember that when you publish your web application to a web server all that will get into the package will be the files of this web application. There's no physical relation to some projects that might have lived into the Visual Studio solution that this web application was part of.

您可以看看 GetManifestResourceStream 方法将允许您从引用的程序集中读取嵌入式XML.

You may take a look at the GetManifestResourceStream method which will allow you to read the embedded XML from the referenced assembly.

这是一个例子:

// you could use any type from the assembly here
var assembly = typeof(TaskManagerHelper).Assembly;
using (var stream = assembly.GetManifestResourceStream("TaskManager.Data.DataBase.TasksDataBase.xml"))
using (var xmlReader = XmlReader.Create(stream))
{
    // ... do something with the XML here
}

请记住,由于文件已嵌入到程序集中,因此您将无法对其进行修改.它是只读的.如果您需要对其进行修改,那么另一种方法是将该文件复制到您的Web应用程序.例如,App_Data特殊文件夹是一个不错的地方.您甚至可以设置一个后编译步骤,该步骤将在此位置复制XML文件.

Bear in mind though that since the file is embedded into the assembly you will not be able to modify it. It is readonly. If you need to modify it then an alternative approach would consist into copying this file to your web application. For example a good place is the App_Data special folder. You could even setup a post compilation step that will copy the XML file in this location.

然后您可以轻松地引用它:

And then you can reference it easily:

string xmlFile = HostingEnvironment.MapPath("~/App_Data/TasksDataBase.xml");
using (var xmlReader = XmlReader.Create(xmlFile))
{
    // ... do something with the XML here
}

在这种情况下,由于XML文件现在实际上是Web应用程序的一部分,并且位于硬盘驱动器上,因此您也可以对其进行修改.

In this case since the XML file is now physically part of the web application and lives on the hard drive you could also modify it.

这篇关于如何在解决方案C#中获取另一个项目的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆