3

I'm not an expert in P4.NET and I would like to show perforce's depot in a treeview (windowsform application c#)...

* "p4 dirs" to get all depots => p4 dirs "//*" for exemple this may give depot1 depot2 ..etc

P4Connection p4 = new P4Connection();
p4.Connect();
P4RecordSet tab1 = p4.Run("dirs","//depot/*"); // to get folders in depot
foreach (P4Record a in tab1 )
{
  richTextBox1.Text += (a["dir"]) + "\n";// show the results in richTextBox

}

* To get a list of files in a directory, run fstat=> p4 fstat "//depot1/*"

P4RecordSet tab2 = p4.Run("fstat","//depot/your_folder/*"); // to get files existing in your_folder
foreach (P4Record b in tab2 )
{
  richTextBox1.Text += (b["depotFile"]) + "\n";// show the results in richTextBox

}

now, how to use this code to build a treeview ? Any help would be most welcome

appiger
  • 31
  • 4

1 Answers1

1

The code below will only support a hardcoded depot, but it wouldn't be hard to extend to look at all depots on a Perforce server by using the "depots" command.

public void PopulateTreeview()
{
    TreeNode depotNode = new TreeNode("//depot");

    P4Connection p4 = new P4Connection();
    p4.Connect();

    ProcessFolder(p4, "//depot", depotNode);

    treeView.Nodes.Add(depotNode);
}

public void ProcessFolder(P4Connection p4, string folderPath, TreeNode node)
{
    P4RecordSet folders = p4.Run("dirs", folderPath + "/*");
    foreach(P4Record folder in folders)
    {
        string newFolderPath = folder["dir"];
        string[] splitFolderPath = newFolderPath.Split('/');
        string folderName = splitFolderPath[splitFolderPath.Length - 1];

        TreeNode folderNode = new TreeNode(folderName);
        ProcessFolder(p4, newFolderPath, folderNode);

        node.Nodes.Add(folderNode);
    }

    P4RecordSet files = p4.Run("fstat", folderPath + "/*");
    foreach(P4Record file in files)
    {
        string[] splitFilePath = file["depotFile"].Split('/');
        string fileName = splitFilePath[splitFilePath.Length - 1];

        TreeNode fileNode = new TreeNode(fileName);
        node.Nodes.Add(fileNode);
    }
}
Mike O'Connor
  • 3,813
  • 24
  • 27
  • thanks for the answer , this solution works for a limited number of files , but in our company we have more than 600 000 files... it's impossible to show the hole depot with this code, it takes so much time. any ideas how to solve this problem ?? – appiger Jul 21 '11 at 09:56
  • I think the only way to fix that is to get rid of the recursion, so you're not trying to parse the entire Perforce tree in one shot. Instead, you could listen for when a node is expanded, and then run ProcessFolder on that node. – Mike O'Connor Jul 21 '11 at 16:00