D
D
Deka0072018-07-01 16:56:45
.NET
Deka007, 2018-07-01 16:56:45

How to make a solution explorer in C#?

Hello. How to make a solution explorer, like in Visual Studio? What classes to use besides TreeView? What to read, where to dig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2018-07-01
@Deka007

What classes are necessary - depends on the task. To list directories and files, no additional classes are needed. The code could be something like this:

treeView1.Items.Clear();
GetFiles("C:/Windows/Microsoft.NET", null);

private void GetFiles(string path, TreeViewItem parent)
{
  var node = new TreeViewItem() { Header = System.IO.Path.GetFileName(path) };
      
  if (parent == null)
  {
    treeView1.Items.Add(node);
  }
  else
  {
    parent.Items.Add(node);
  }

  var attr = System.IO.File.GetAttributes(path);

  if (attr.HasFlag(System.IO.FileAttributes.Directory))
  {
    var directories = System.IO.Directory.GetDirectories(path);

    foreach (var dir in directories)
    {
      GetFiles(dir, node);
    }

    var files = System.IO.Directory.GetFiles(path);

    foreach (var file in files)
    {
      node.Items.Add(System.IO.Path.GetFileName(file));
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question