How to embed an XSLT file in a .NET project to be included in the output .exe?

早过忘川 提交于 2019-12-04 21:31:10

问题


I have a simple C# Console App that reads in an XML file specified the the user, runs an XSLT transformation on it, and outputs the results.

When I distribute my app to users, I want to distribute a single .EXE file. My source code consists of 3 files: the .csproj file, the .cs code file, and a .xslt stylesheet.

How can I set up the csproj so the .xslt is "embedded" within the output and cannot be seen or modified by the end user?

Seems easy, but I can't figure it out and Google isn't being too useful.


回答1:


You can embedd it in your assembly.

Add the file in your solution, set the build action to embedded resource.

The place you need to read the file use http://msdn.microsoft.com/en-us/library/xc4235zt.aspx Assembly.GetManifestResourceStream wich will give you a stream wich you can write to a fil or use directly.

If you are not quite sure what name your resource have, I find Assembly.GetManifestResourceNames usefull to list all resources.




回答2:


Add the file to your project, then select the file and go to the Properties window (press F4). Set the build action to "Embedded resource". This will cause the file to be embedded into the exe file as a resource.

using(Stream strm = Assembly.GetExecutingAssembly().GetManifestResourceStream("YourAssemblyName.filename.xslt"))
using (XmlReader reader = XmlReader.Create(strm))
{
    XslTransform transform = new XslTransform();
    transform.Load(reader);
    // use the XslTransform object
}


来源:https://stackoverflow.com/questions/1749017/how-to-embed-an-xslt-file-in-a-net-project-to-be-included-in-the-output-exe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!