2013-06-05 22 views
5

Próbuję programowo utworzyć element projektu. Mam ten kodVisual Studio: programowo Utwórz elementy projektu w katalogu projektu

  string itemPath = currentSolution.GetProjectItemTemplate("ScreenTemplate.zip", "csproj"); 
     currentSolution.Projects.Item(1).ProjectItems.AddFromTemplate(itemPath, name); 
     currentSolution.Projects.Item(1).Save(); 

Ale chciałbym, aby utworzyć pozycję w określonym katalogu wewnątrz projektu (to tworzy element w głównym katalogu projektu). Czy to możliwe? Dzięki za pomoc!

Odpowiedz

1

Z grubsza to, w jaki sposób dodaję mój plik cpp, nie powinno być inaczej w twoim przypadku.

Kod doda plik pod "SourceFiles \ SomeFolder" w projekcie, a także w folderze "Source Files" w drzewie widoku projektu (powinno już być tam).

Project project = null; // you should get the project from the solution or as active project or somehow else 
string fileName = "myFileName.cpp"; 
string fileRelativePath = "SourceFiles\\SomeFolder\\" + fileName; 

// First see if the file is already there and delete it (to create an empty one) 
string fileFullPath = Path.GetDirectoryName(project.FileName) + "\\" + fileRelativePath; 
if (File.Exists(fileFullPath)) 
    File.Delete(fileFullPath); 

// m_applicationObject here is DTE2 or DTE2 
string templatePath = (m_applicationObject.Solution as Solution2).ProjectItemsTemplatePath(project.Kind); 

ProjectItem folderItem = project.ProjectItems.Item("Source Files"); 
ProjectItem myFileItem = folderItem.ProjectItems.AddFromTemplate(templatePath + "/newc++file.cpp", fileRelativePath); 

Proszę nie oczekiwać, że kod, aby skompilować i uruchomić od razu - niektórzy sprawdza nieprawidłowym stanie nie są wykonywane tutaj.

Powiązane problemy