简体   繁体   English

如何在预构建事件中将源文件(.cs)添加到资源(.resx)?

[英]How to add source file (.cs) to resources (.resx) in a pre-build event?

Is it simple or even possible? 简单甚至可能吗?

Why : Following my other question and using this approach , now I would like to simply embed the source file to the resources so I can copy it to the temp folder while the application is running - and keep the application as a single file. 原因 :按照我的其他问题并使用这种方法 ,现在,我想简单地将源文件嵌入资源中,以便可以在应用程序运行时将其复制到temp文件夹中-并将应用程序保留为单个文件。

I assume using a pre-build event is the best way to do this. 我认为使用预构建事件是最好的方法。 If so, how would it be done? 如果是这样,将如何做?

EDIT : I'm asking about adding it before building because I also assume it won't be automatically updated if I add it manually once and then change the code after. 编辑 :我正在询问在构建之前添加它,因为如果我手动添加一次然后在之后更改代码,我还假设它不会自动更新。

I recently went through this same issue when developing an examples suite for a .NET control which had to display its own example source code in the application. 我最近在为.NET控件开发示例套件时遇到了同样的问题 ,该套件必须在应用程序中显示其自己的示例源代码。

The approach I ended up with was as follows. 我最终采用的方法如下。

  1. I created a batch script (run in pre-build) to copy all the code files under /Examples/* to another folder in the solution Resources/ExamplesSrc/* 我创建了一个批处理脚本(在预构建中运行)以将/ Examples / *下的所有代码文件复制到解决方案Resources / ExamplesSrc / *中的另一个文件夹中

  2. I then included all these files under Resources/ExamplesSrc/* in the csproj and saved it. 然后,我将所有这些文件包括在csproj中的Resources / ExamplesSrc / *下并保存了。

  3. The next step was I set all the files under Resources/ExamplesSrc/* as embedded resource and committed the change to SVN. 下一步是我将Resources / ExamplesSrc / *下的所有文件设置为嵌入式资源,并将更改提交给SVN。 I did not commit the examples (generated) to SVN, just the modified csproj and batch file. 我没有将示例(生成的)提交给SVN,仅提交了修改后的csproj和批处理文件。

The effect when developers checked out was that they had (!) icons on all the resources (they were not on disk, but the csproj was looking for them), however as soon as they built the generated files appeared and all was well. 开发人员签出时的结果是,它们在所有资源上都有(!)图标(它们不在磁盘上,但是csproj正在查找它们),但是,一旦他们构建了生成的文件,它们就会出现,并且一切都很好。 This approach also worked with a build server (Team city) and the examples suite can now load and browse its own code via embedded resources. 这种方法也适用于构建服务器(Team city),示例套件现在可以通过嵌入式资源加载和浏览自己的代码。

One caveat you should be aware of. 您应该注意的一个警告。 There is a quirk (by design apparently) in VS2010. VS2010中有一个古怪(显然是设计使然)。 If an embedded resource has ".cs" in it (ie any code file!) it cannot be loaded from the assembly using Assembly.GetManifestResourceStream(). 如果嵌入式资源中包含“ .cs”(即任何代码文件!),则无法使用Assembly.GetManifestResourceStream()从程序集中加载该资源。 To work around this all source files were renamed in the batch copy step from *.Xaml.cs to *.Xaml.c.txt, from *.cs to *.c.txt 要解决此问题,在批处理复制步骤中,所有源文件都从* .Xaml.cs重命名为* .Xaml.c.txt,从* .cs重命名为* .c.txt

Hope this helps! 希望这可以帮助!

Turns out I don't need to (re)add the source file each time I build the solution! 事实证明,每次构建解决方案时,我都不需要(重新)添加源文件!

When I add it manually once it becomes a "Text" file on resources - and I can easily access it as a string using Properties.Resources.SourceCode (having the file named "SourceCode.cs"). 当我手动添加它后,它成为资源上的“文本”文件-可以使用Properties.Resources.SourceCode (具有名为“ SourceCode.cs”的文件)轻松地以字符串形式访问它。 And yes, it is updated automatically since the resource property "Persistence": the file is linked at compile time. 是的,由于资源属性“ Persistence”:文件在编译时已链接,因此会自动更新。

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

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