Create Visual Studio Project with API from matlab

조회 수: 3 (최근 30일)
Matteo Boccino
Matteo Boccino 2020년 2월 21일
답변: Aniket 2025년 4월 8일
Hi everyone, I have to create a visual studio project (programmatically) from Matlab, using the API present in this site
https://docs.microsoft.com/en-us/dotnet/api/envdte.projects?view=visualstudiosdk-2017. In the last folder of the path below, there are files .h and .c. Anyone has encountered a problem like this? I'm using Visual Studio 2017 and Matlab 2019b.
I show what are the APIs that I'm using that don't work:
openedVS = actxGetRunningServer('VisualStudio.DTE.15.0');
sol=openedVS.DTE.Solution;
sol.Create('C:\Users\matte\Desktop\VisualStudio','MyNewSolution');
sol.SaveAs('C:\Users\matte\Desktop\VisualStudio\MyNewSolution.sln');
proj=sol.AddFromTemplate('C:\Users\matte\Desktop\VisualStudio\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.vcxproj','C:\Users\matte\Desktop\VisualStudio','My New Project',true);
proj.ProjectItems.AddFromDirectory('C:\Users\matte\Desktop\Tesi\file generazione\file generati\');
On the last row of code I obtain the followng error:
Error using Interface.8E2F1269_185E_43C7_8899_950AD2769CCF/AddFromDirectory
Invoke Error, Dispatch Exception: Non implementato
Error in Creazione_Progetto (line 7)
proj.ProjectItems.AddFromDirectory('C:\Users\matte\Desktop\Tesi\file generazione\file generati\');

답변 (1개)

Aniket
Aniket 2025년 4월 8일
I am able to reproduce the error you are facing. This is actually expected behavior because ProjectItems.AddFromDirectory() is not implemented for all project types (especially VC++ projects), or not exposed through the DTE automation layer.
As a workaround, instead of using AddFromDirectory(), you’ll need to add the .c and .h files individually using AddFromFile() or AddFromFileCopy() as shown below:
folderPath = 'C:\Users\matte\Desktop\Tesi\file generazione\file generati\';
files = dir(fullfile(folderPath, '*.c'));
files = [files; dir(fullfile(folderPath, '*.h'))];
for k = 1:length(files)
fullPath = fullfile(folderPath, files(k).name);
proj.ProjectItems.AddFromFile(fullPath); % Or AddFromFileCopy if you want a copy
end
Also note that if you're trying to add these to a specific folder inside the project, like proj.ProjectItems.Item("Source Files").ProjectItems.AddFromFile(...), you’ll need to navigate through the project tree.
I hope this helps resolve the issue!

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by