简体   繁体   中英

C# File Relative Path to a class

I have a C# class file which will be packaged as a class library. In the class library project, I have a folder called XSL and inside this folder I have some XSL files. My C# class which does XSL validation is in a different namespace. 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?

If the files are embedded in the Assembly, you can get their content ( Stream ) using the Assembly.GetManifestResourceStream(String) method.

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 a = Assembly.LoadFrom("file.dll");

This of course only works if it is a .NET assembly with a version that can be interpreted. So don't use it to load a .exe generated by a C-compiler.

Each resource is stored by a dot-separated path, for instance "MyCompany.MyProduct.MyDirectory.MyFile.xslt"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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