2013-06-05 6 views
5

プログラムアイテムをプログラムで作成しようとしています。私はこのコードVisual Studio:プロジェクトディレクトリにプロジェクト項目をプログラムで作成する

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

を持っているしかし、私は(これは、プロジェクトのルートに項目を作成します)プロジェクト内の指定したディレクトリにアイテムを作成したいと思います。出来ますか?手伝ってくれてありがとう!

答えて

1

これはおおまかに私のcppファイルを追加する方法であり、あなたの場合も変わらないはずです。

このコードは、プロジェクト内の「SourceFiles \ SomeFolder」の下にファイルを追加し、プロジェクトビューツリーの「ソースファイル」フォルダにファイルを追加します(すでにそこにあるはずです)。

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); 

コードがすぐにコンパイルされ実行されることを期待しないでください。ここで無効な状態のチェックは行われません。

関連する問題