Create Visual Studio Project with API from matlab

8 Ansichten (letzte 30 Tage)
Matteo Boccino
Matteo Boccino am 21 Feb. 2020
Beantwortet: Aniket am 8 Apr. 2025
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\');

Antworten (1)

Aniket
Aniket am 8 Apr. 2025
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!

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by