简体   繁体   English

我无法从第二个C#项目中读取我的嵌入式XML文件

[英]I can't read my embedded XML file from my second C# project

I have a Visual Studio Solution with two projects inside. 我有一个Visual Studio解决方案,里面有两个项目。

The first project is a windows service, and this project contains one XML file inside a folder (called Configurations). 第一个项目是一个Windows服务,该项目在一个文件夹(称为配置)中包含一个XML文件。 This XML file is called Databases.xml. 此XML文件称为Databases.xml。 I have changed Databases.xml Build Action from content to embedded resource, and now I want to access this XML file from my other project in my solution, which is a WPF application. 我已将Databases.xml Build Action从内容更改为嵌入式资源,现在我想从我的解决方案中的其他项目访问此XML文件,这是一个WPF应用程序。

I have therefore added an reference to my windows service project inside my WPF project, and would now like to access my XML file from my WPF project. 因此,我在WPF项目中添加了对我的Windows服务项目的引用,现在想从我的WPF项目中访问我的XML文件。

My problem is that when I am trying to access the embedded resource then I can't find out which type to use and what the path/namespace to my assembly and XML file should be. 我的问题是,当我尝试访问嵌入式资源时,我无法找出要使用的类型以及我的程序集和XML文件的路径/命名空间应该是什么。 When I am using the 当我使用的时候

string[] names = this.GetType().Assembly.GetManifestResourceNames();

my names array is filled out with some resources from my WPF project. 我的名字数组中填充了我的WPF项目中的一些资源。 What I want is to access the ResourceNames and of course my Databases.xml file from my Windows Service project. 我想要的是从我的Windows服务项目访问ResourceNames,当然还有我的Databases.xml文件。

Please help me since this problem is driving me nuts. 请帮助我,因为这个问题让我疯了。

If you need any additional information, please let me know. 如果您需要任何其他信息,请告诉我们。

My Solution Update 26-07-2013 我的解决方案更新26-07-2013

I found out that the real problem occured when I couldn't use my first windows Service projects namespace as a type for my assembly. 我发现当我无法使用我的第一个Windows服务项目命名空间作为我的程序集的类型时,出现了真正的问题。 my Windows Service consists of a service class (with OnStart() and OnStop() method inside), and in order to use this class as my namespace type, I needed to add another reference to my WPF project. 我的Windows服务包含一个服务类(里面有OnStart()和OnStop()方法),为了使用这个类作为我的命名空间类型,我需要添加另一个对我的WPF项目的引用。 I needed to add a reference to System.ServiceProcess namespace, in order to use my Windows Service Class as a type for my assembly in my WPF Project. 我需要添加对System.ServiceProcess命名空间的引用,以便在我的WPF项目中将我的Windows服务类用作我的程序集的类型。

In Order to access my Databases.xml file, I have come up with this solution. 为了访问我的Databases.xml文件,我提出了这个解决方案。 Remember to insert your own projects name and class name instead of my placeholders (<Windows Service Project Name> etc). 请记住插入您自己的项目名称和类名而不是我的占位符(<Windows服务项目名称>等)。

//Remember to add a reference to System.ServiceProcess in order to be able to use your WIndows Service Project as an assembly type.
using (Stream stream = typeof(<Windows Service Project Name>.<Windows Service Class Name>).Assembly.GetManifestResourceStream("<Windows Service Project Name>.<Folder Name>.Databases.xml"))
{
    //Load XML File here, for instance with XmlDocument Class
    XmlDocument doc = new XmlDocument();
    doc.Load(stream);
}

So my real problem was that I didn't include the System.ServiceProcess reference to my second project. 所以我真正的问题是我没有将System.ServiceProcess引用包含到我的第二个项目中。

You've to refer to Windows Service project Assembly to make it work 您需要参考Windows Service project Assembly才能使其正常工作

The problem with your code is This.GetType().Assesmbly gives current Assembly In your case WPF Assembly and obviously you'll not find what you need there. 您的代码的问题是This.GetType().Assesmbly提供当前Assembly在您的情况下WPF Assembly ,显然您将找不到您需要的内容。

Try this 尝试这个

Assembly windowsServiceAssembly = typeof(SomeTypeFromThatAssembly).Assembly;
string[] names = windowsServiceAssembly.GetManifestResourceNames();

Hope this helps. 希望这可以帮助。

If your class is static class then use this methods: 如果您的类是静态类,则使用以下方法:

internal static string GetFromResources(string resourceName)
    {
        var asm = Assembly.GetExecutingAssembly();

        var resource = asm.GetManifestResourceNames().First(res => res.EndsWith(resourceName, StringComparison.OrdinalIgnoreCase));

        using (var stream = asm.GetManifestResourceStream(resource))
        {
            if (stream == null) return string.Empty;

            using (var reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }
    }

For example if your embedded resource file names on the this project is 'MyFile.txt' then use this static method same this code: 例如,如果此项目中的嵌入式资源文件名为“MyFile.txt”,则使用此静态方法,此代码相同:

var myFileData = GetFromResources("MyFile.txt");

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

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