4

I've programatically created a number of files. I want to add them to the visual studio project. I have a reference to the project (DTE.Project). When I execute the code below, I get an error message.

project.ProjectItems.AddFromFile(filename);

The error is

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)

When I wrap the call in a try-catch, the process runs to completion but non of the files are added to the project. I have to manually show hidden files and then include them in the project.

How do I add the file for this to work?

ritcoder
  • 3,274
  • 10
  • 42
  • 62
  • I just read from the msdn documentation that ProjectItems.AddFromFile will fail if the file already exists in the project. What other function is available for including a file in a project? – ritcoder Jan 16 '12 at 08:38
  • 2
    This is an odd question... If the file already exists in the project, then *you don't need to add the file to the project.* All you need to do is iterate the ProjectItems at the location you wish to add the file and, if it already exists, skip adding it. –  Jan 16 '12 at 14:43
  • I think this thread provides the solution for your question - [Visual Studio macro: Find files that aren't included in the project?][1] [1]: http://stackoverflow.com/questions/2000197/visual-studio-macro-find-files-that-arent-included-in-the-project – Dmitry Pavlov Feb 21 '12 at 07:46
  • @Will: It seems odd but I'm using a script that is generating a list of files and adds them to the project. Since I'm relatively new to DTE and stuff, I thought there was a method I'm missing. – ritcoder Feb 22 '12 at 20:33
  • @DmitryPavlov: Seems like there is no method to call but indeed the page does contain a solution for me. Can you add it as an answer so I accept it. thanks. – ritcoder Feb 22 '12 at 20:36

1 Answers1

0

@ritcoder - sure:

I think this thread provides the solution for your question - Visual Studio macro: Find files that aren't included in the project?

Community
  • 1
  • 1
Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121