2

How can I add existing item in solution explorer programmatically (i.e., at run time) in a Visual studio 2010 project ?

existing items may include images,videos etc

Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
user1106633
  • 155
  • 2
  • 12
  • Your question is unclear to me. You are asking how to add files and folders to a project (or solution folder?) while a solution and its projects are loaded in visual studio? – Ritch Melton Mar 25 '12 at 02:53
  • i want to add files to solution folder of a project programatically @RitchMelton – user1106633 Mar 25 '12 at 03:00
  • Projects don't have solution folders, solutions do. – Ritch Melton Mar 25 '12 at 03:03
  • oh sorry, actually i want to add existing items from my pc to project folder programatically – user1106633 Mar 25 '12 at 03:05
  • Duplicate of [http://stackoverflow.com/questions/9852746/dynamically-add-a-file-in-project-using-c-sharp](http://stackoverflow.com/questions/9852746/dynamically-add-a-file-in-project-using-c-sharp) – Raj Ranjhan Mar 25 '12 at 03:25
  • The question is tagged C# so as such I'm assuming you need to do this from C# code, but if you ever need to do it via something closer to scripting, using the PowerConsole extension is very easy and powerful IME. http://visualstudiogallery.msdn.microsoft.com/67620d8c-93dd-4e57-aa86-c9404acbd7b3 – James Manning Mar 25 '12 at 05:04

1 Answers1

1

You'll likely end up using Visual Studio's existing automation framework for this, or something that wraps it.

In particular, once you get a reference to the project, you'll access its ProjectItems property to get the collection of items in the project, and then use either AddFromFile or AddFromFileCopy depending on whether you want to add the original at its existing location or a copy to be placed within the project dir.

the DTE object is kind of the 'root' of the automation object model - for more info, see http://msdn.microsoft.com/en-us/library/t6d9sf9k.aspx

James Manning
  • 13,429
  • 2
  • 40
  • 64