I was looking for the ability to nest a folder in solution explorer directly by code, and I found the function AddFromDirectory
to achieve the result (I don't want to deal too much with xml parser).
The main problem is related at the fact that I can only add folder to the main project solution folder, but I don't understand how to attach new folder under already existing one.
Image to explain better:
I'd like to create the TEST
folder, under the src
folder, but i didn't understand how.
I'm using the following code:
....
....
string pathx = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), @"../../App.config");
// Get a reference to the current instance of Visual Studio
DTE dte = (DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");
// Get a reference to the solution object
Solution solution = (Solution)dte.Solution;
// Get a reference to the project you want to add the folder to
Project project = solution.Projects.Item(1); // Replace 1 with the index or name of your project
string parentFolder = Directory.GetParent(Assembly.GetEntryAssembly().Location).Parent.Parent.FullName;
string concatFolder = $@"src\TEST";
string LocalFolder = Path.Combine(parentFolder, concatFolder);
if (!Directory.Exists(LocalFolder))
{
try
{
Directory.CreateDirectory(LocalFolder); // local folder
project.ProjectItems.AddFromDirectory(LocalFolder); // assembly reference for visual studio
}
catch(Exception ex)
{
MessageBox.Show($"{ex}");
}
}
I've tried to deal with the parameter LocalFolder
, but I didn't manage to make it works.
Someone have an idea about how to achieve this ?