如何以编程方式在我的项目中包含文件? [英] How to programmatically include a file in my project?

查看:94
本文介绍了如何以编程方式在我的项目中包含文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个帮助程序,该程序可以重新格式化一些代码文件并创建新的代码文件,这些文件将被添加到我的其他项目中,因此我可以立即使用新代码,但是我在添加该代码时遇到了严重的麻烦新的代码文件自动进入我的项目.顺便说一下,它在c#中,而辅助应用程序是WinForms.

I'm making a helper application that reformats some code files and creates new code files, which are to be added to my other project, so I could use the new code right away, but I'm having serious trouble adding that new code file into my project automatically. By the way it's in c# and the helper app is WinForms.

此问题的唯一答案有两种解决方法,但是我不能不要让它们中的任何一个起作用.对于第一个,我找不到要引用的Microsoft.Build程序集,而在第二个中,显然没有足够的参数用于命令行.

This question's only answer has two ways of doing that, but I couldn't make any of them work. With the first I can't find a Microsoft.Build assembly to reference, and in the other there are clearly not enough arguments for a command line.

如何在不使用第三方应用程序的情况下以编程方式将文件包含到项目中?

How do I programmatically include a file into a project without the use of third-party applications?

基本上,我正在寻找与之等效的东西:

Basically, I'm looking for the equivalent of this:

...但是使用代码完成了.

...But done using code.

这些是我认为解决方案应该提供的功能:

These are the features I suppose the solution should offer:

  • 选择具有 project solution ,我们将文件添加到
  • 选择要在其中添加文件的 project
  • 在项目中选择目录
  • 当然还有我们要添加的文件
  • Select the solution which has the project we're adding the file to
  • Select project into which the file is to be added
  • Select directory within the project
  • And, of course, the file which we're adding

在用户@ psubsee2003的帮助下,我能够在计算机上的C:\Windows\Microsoft.NET\Framework\v4.0.30319文件夹中找到Microsoft.Build.dll文件,并通过将项目的目标框架更改为版本4 完整配置文件来成功导入了该文件默认的客户端配置文件.

With user @psubsee2003's help I was able to find the Microsoft.Build.dll file in C:\Windows\Microsoft.NET\Framework\v4.0.30319 folder on my computer and successfully import it by changing my project's target framework to version 4 Full profile, not the default Client profile.

我发现了如何使用AddItem方法:

And I found how to use the AddItem method:

var p = new Microsoft.Build.Evaluation.Project(@"C:\projects\MyProject.csproj");
p.AddItem("Compile", @"C:\folder\file.cs");
p.Save();

除非项目已经有一个名为folder的文件夹,否则该文件将出现在项目的根文件夹中,在这种情况下,文件将放置在该文件夹中.因此,基本上,该文件将被放置在原始文件路径中朝向根文件夹的最深文件夹链中.

The file will appear in project's root folder unless the project already had a folder called folder, in which case the file will be placed there. So basically the file will be placed in the deepest folder chain found in the original file's path going towards the root folder.

推荐答案

它适用于我,只需将其添加到ProjectFolder,然后以编程方式添加该文件夹即可.

It worked for my just adding the it to the ProjectFolder, and also add the folder programmatically like this.

var p = new Microsoft.Build.Evaluation.Project(@"C:\projects\BabDb\test\test.csproj");
        p.AddItem("Folder", @"C:\projects\BabDb\test\test2");
        p.AddItem("Compile", @"C:\projects\BabDb\test\test2\Class1.cs");
        p.Save();

这篇关于如何以编程方式在我的项目中包含文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆