0

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:

enter image description here

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 ?

Zenek
  • 60
  • 2
  • 12
  • Does an error occur if you directly cut the folder into a new folder? – wenbingeng-MSFT Aug 10 '23 at 10:04
  • Nope, just spawn it in the `main solution explorer`, as in the example. Basically it doesn't count where the folder is, but always spawn it in the main `working tree` – Zenek Aug 11 '23 at 17:30

0 Answers0