2012-01-15 148 views
4

我编程创建了一些文件。我想将它们添加到Visual Studio项目中。我有一个参考项目(DTE.Project)。当我执行下面的代码时,我收到一条错误消息。使用DTE将现有文件添加到Visual Studio项目

project.ProjectItems.AddFromFile(filename); 

的错误是

Exception at 'Expand' - 'Unable to add 'manage.txt'. A file with that name already exists.' 
System.Runtime.InteropServices.COMException (0x80040400): Unable to add 'manage.txt'. A file with that name already exists. 
at EnvDTE.ProjectItems.AddFromFile(String FileName) 

当我包装在一个try-catch调用,进程运行到完成但文件不被添加到项目中。我必须手动显示隐藏的文件,然后将它们包含在项目中。

如何添加文件以使其工作?

+0

我刚刚从MSDN文档,如果该文件中的项目已经存在ProjectItems.AddFromFile将无法读取。 还有什么其他功能可用于在项目中包含文件? – ritcoder 2012-01-16 08:38:15

+2

这是一个奇怪的问题......如果文件已经存在于项目中,那么*不需要将该文件添加到项目中*您只需在要添加的位置迭代ProjectItems该文件,如果它已经存在,则跳过添加它。 – Will 2012-01-16 14:43:51

+0

我觉得这个线程提供了你的问题解决 - [Visual Studio的宏:查找未包含在项目文件] [1] [1]:http://stackoverflow.com/问题/ 2000197/visual-studio-macro-find-files-that-arent-included-in-the-project – 2012-02-21 07:46:20

回答

相关问题