2013-06-05 38 views
5

Tôi đang cố gắng tạo một mục dự án theo chương trình. tôi có mã nàyVisual Studio: Lập trình tạo các mục dự án trong thư mục dự án

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

Nhưng tôi muốn tạo mục trong thư mục được chỉ định bên trong dự án (mục này tạo mục trong thư mục gốc của dự án). Có thể không? Cảm ơn vì sự giúp đỡ!

Trả lời

1

Đó là khoảng cách tôi thêm tệp cpp của mình, sẽ không khác gì trong trường hợp của bạn.

Mã sẽ thêm tệp trong "SourceFiles \ SomeFolder" trong dự án và cũng trong thư mục "Tệp nguồn" trong cây xem dự án (cần phải có sẵn).

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

Vui lòng không mong đợi mã biên dịch ngay và chạy - một số kiểm tra trạng thái không hợp lệ không được thực hiện tại đây.

Các vấn đề liên quan