简体   繁体   English

类的C#文件相对路径

[英]C# File Relative Path to a class

I have a C# class file which will be packaged as a class library. 我有一个C#类文件,该文件将打包为类库。 In the class library project, I have a folder called XSL and inside this folder I have some XSL files. 在类库项目中,我有一个名为XSL的文件夹,并且在此文件夹中有一些XSL文件。 My C# class which does XSL validation is in a different namespace. 我执行XSL验证的C#类位于其他名称空间中。 How can I refer to the XSL folder from a different namespace in which I have the class that wants to do the XSL validation by using the XSL files? 如何从不同的名称空间引用XSL文件夹,在该名称空间中我有想要通过使用XSL文件进行XSL验证的类?

If the files are embedded in the Assembly, you can get their content ( Stream ) using the Assembly.GetManifestResourceStream(String) method. 如果文件嵌入在Assembly中,则可以使用Assembly.GetManifestResourceStream(String)方法获取其内容( Stream )。

So: 所以:

Assembly a = Assembly.GetExecutingAssembly(); //get access to the current assembly.
Stream s = a.GetManifestResourceStream("xslt.file1");
//process stream s

In case it is a third party assembly (class library) you can load it by creating an Assembly instance from that path: 如果它是第三方程序集(类库),则可以通过从该路径创建一个Assembly实例来加载它:

Assembly a = Assembly.LoadFrom("file.dll");

This of course only works if it is a .NET assembly with a version that can be interpreted. 当然,仅当它是具有可解释版本的.NET程序集时,此方法才有效。 So don't use it to load a .exe generated by a C-compiler. 因此,请勿使用它加载由C编译器生成的.exe

Each resource is stored by a dot-separated path, for instance "MyCompany.MyProduct.MyDirectory.MyFile.xslt" 每个资源都由点分隔的路径存储,例如"MyCompany.MyProduct.MyDirectory.MyFile.xslt"

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

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