locked
Folder searcher RRS feed

  • Question

  • Hello, 

    what should i use if i want to create GUI app with ability to browse folder, where ROOT folder would be like C:\TMP and everything else upon C drive will be hidden?

    FolderBrowserDialog class looks like unusable for my needs. 

    The GUI will be shared by colleagues and they will have the ability to create server shares through the app. I want them only to be able to create/delete share and create them only under restricted location.

    Regards

    Friday, January 26, 2018 12:00 AM

Answers

  • I posted  second link with specific examples of how to use PowerShell with Forms and how to design Forms with PowerShell. I wanted to warn you that forms design requires advanced skills with PowerShell and it requires good programming skills with the fundamentals of Windows FOrms.  The question you asked shows  that you may need more tools in your toolbox.

    Read the articles I posted a link to.  If your PS skills are very good they will answer all of your questions.

    And yes,  the FolderBrowserDialog is one control you can used to create a share.


    \_(ツ)_/

    • Marked as answer by Mekac Friday, January 26, 2018 7:40 AM
    Friday, January 26, 2018 7:13 AM
  • Here is a better one that show how to further enable and event a TreeView to display information or access more information with almost no code.

    https://1drv.ms/u/s!AjiiPtIUqzK_hcp-4sz38ZY1f0B8Gw


    \_(ツ)_/

    • Marked as answer by Mekac Saturday, January 27, 2018 12:07 AM
    Friday, January 26, 2018 8:48 PM

All replies

  • You would need to learn PowerShell to a fairly high level of proficiency and then learn Windows forms.

    You could, however, just do it without a GUI.

    Start here: Learn PowerShell  

    After that it should take you about a week to write your first complex script.

    You can also look here for tips and documentation: https://info.sapien.com/index.php/guis/gui-scripting


    \_(ツ)_/

    Friday, January 26, 2018 12:25 AM
  • Hi,

    usually I agree with your generalized copy&paste answers.

    However, I would expect this answer to someone who just registered to this forum and have really newbish question.

    I think I'm past beyond watching "How to run powershell" tutorials. I am NOT "PRO" or somewhere nearby this level, but I already have some experience.   .. I just didn't need to restrict FolderBrowser to show only what I need.

    You would need to learn PowerShell to a fairly high level of proficiency and then learn Windows forms.

    I agree, otherwise I would be asking such a question. 

    You could, however, just do it without a GUI.

    Not sure what u mean by that. Getting directory collection outside GUI and then populate data into TreeView for example..?

    Start here: Learn PowerShell  

    Very basic stuff ..

    I think Ill try my luck with TreeView - ill post my result if I make significant progress.

    Friday, January 26, 2018 7:04 AM
  • I posted  second link with specific examples of how to use PowerShell with Forms and how to design Forms with PowerShell. I wanted to warn you that forms design requires advanced skills with PowerShell and it requires good programming skills with the fundamentals of Windows FOrms.  The question you asked shows  that you may need more tools in your toolbox.

    Read the articles I posted a link to.  If your PS skills are very good they will answer all of your questions.

    And yes,  the FolderBrowserDialog is one control you can used to create a share.


    \_(ツ)_/

    • Marked as answer by Mekac Friday, January 26, 2018 7:40 AM
    Friday, January 26, 2018 7:13 AM
  • Here is an advanced example that is not what you want but shows some excellent techniques for building GUI scripts.

    http://tech-comments.blogspot.com/2017/03/powershell-understanding-windows-forms.html

    There are three articles with links to each at the bottom.


    \_(ツ)_/

    Friday, January 26, 2018 7:53 AM
  • Still a little buggy, but works (i just need another event (onClick)

    function SetRoot {
    	# set the root
        $rootfolder = Get-Item "D:\SCRIPTS"	
        $treeview1.Nodes.Clear()
    	$node=New-Object System.Windows.Forms.TreeNode
    	$node.Name=$rootfolder.FullName
    	$node.Text=$rootfolder.Name
    	$node.Tag =$rootfolder.FullName
        $treeview1.Nodes.Add($node)
        Invoke-Command $treeview1_doubleclick
    }
    
    
    # doubleclick event inside treeview
    $treeView1_doubleclick = {
    [System.Windows.Forms.Application]::UseWaitCursor = $true
    if ($treeView1.SelectedNode -eq $null)
     {
      $node = $treeView1.Nodes[0]
     }
     else 
     {
      $node = $treeView1.SelectedNode
      
     }
        if($node.Nodes.Count -eq 0){
    			    $SearchRoot="$($node.Tag)"
                    $DirList = get-childitem $SearchRoot -Directory
                    foreach ($Directory in $DirList){
                        $DirName = $Directory.Name
                        $FullName = $Directory.FullName
            	        $newnode=New-Object System.Windows.Forms.TreeNode 
    			        $newnode.Name=$DirName
    			        $newnode.Text=$DirName
    			        $newnode.Tag=$FullName
    			        $node.Nodes.Add($newnode)			
                        $statusBar1.Text = $treeView1.SelectedNode.Tag
                        }
                    }
    			        $node.Expand()
                        [System.Windows.Forms.Application]::UseWaitCursor = $False
                        
    }
    
    $OnLoadForm_StateCorrection=
    {#Correct the initial state of the form to prevent the .Net maximized form issue
    	$form1.WindowState = $InitialFormWindowState
        SetRoot
    }
    

    Friday, January 26, 2018 12:44 PM
  • Of course you realize all of that can be done with the FolderBrowserDialog configuration properties.  You can set the root ad most other parameters.


    \_(ツ)_/

    Friday, January 26, 2018 8:23 PM
  • To invoke a script block:

    $treeView1_doubleclick.Invoke()

    To invoke with arguments:

    $treeView1_doubleclick.Invoke($arg1,$arg2, ... )

    To invoke as a delegate:

    $treeview1.Invoke($scriptblock)


    \_(ツ)_/




    • Edited by jrv Friday, January 26, 2018 8:32 PM
    Friday, January 26, 2018 8:27 PM
  • Here is a full example of how to dynamically load a TreeView.

    https://1drv.ms/u/s!AjiiPtIUqzK_hcp4e07Wo-YxcjRXHw


    \_(ツ)_/

    Friday, January 26, 2018 8:36 PM
  • Here is a better one that show how to further enable and event a TreeView to display information or access more information with almost no code.

    https://1drv.ms/u/s!AjiiPtIUqzK_hcp-4sz38ZY1f0B8Gw


    \_(ツ)_/

    • Marked as answer by Mekac Saturday, January 27, 2018 12:07 AM
    Friday, January 26, 2018 8:48 PM
  • Of course you realize all of that can be done with the FolderBrowserDialog configuration properties.  You can set the root ad most other parameters.


    \_(ツ)_/

    Hm.. when i played around with FolderBrowserDialog, i could set the root (starting position) but nothing stops user to go to parent folder or different disk drive.    Maybe i overlooked something. 
    Saturday, January 27, 2018 12:10 AM
  • using assembly System.Windows.Forms
    using namespace System.Windows.Forms
    $fbd = [folderbrowserdialog]::New()
    $fbd.RootFolder = [System.Environment+SpecialFolder]::UserProfile
    $fbd.ShowNewFolderButton = $false
    $fbd.SelectedPath = 'C:\Users\jvierra\Favorites\Computer Science'
    $fbd.ShowDialog()
    


    \_(ツ)_/

    Saturday, January 27, 2018 12:44 AM
  • This one is configurable in any way you want and it can use all explorer link types.

    Add-Type -ReferencedAssemblies ('System.Windows.Forms') -TypeDefinition @'
    using System;
    using System.Windows.Forms;
    using System.Reflection;
    
    public class FolderBrowserModernDialog : System.Windows.Forms.CommonDialog
    {
        private System.Windows.Forms.OpenFileDialog fileDialog;
        public FolderBrowserModernDialog()
        {
            fileDialog = new System.Windows.Forms.OpenFileDialog();
            fileDialog.Filter = "Folders|\n";
            fileDialog.AddExtension = false;
            fileDialog.CheckFileExists = false;
            fileDialog.DereferenceLinks = true;
            fileDialog.Multiselect = false;
            fileDialog.Title = "Select a folder";
        }
    
        public string Title
        {
            get { return fileDialog.Title; }
            set { fileDialog.Title = value; }
        }
    
        public string InitialDirectory
        {
            get { return fileDialog.InitialDirectory; }
            set { fileDialog.InitialDirectory = value; }
        }
        
        public string SelectedPath
        {
            get { return fileDialog.FileName; }
            set { fileDialog.FileName = value; }
        }
    
        object InvokeMethod(Type type, object obj, string method, object[] parameters)
        {
            MethodInfo methInfo = type.GetMethod(method, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            return methInfo.Invoke(obj, parameters);
        }
    
        bool ShowOriginalBrowserDialog(IntPtr hwndOwner)
        {
            using(FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
            {
                folderBrowserDialog.Description = this.Title;
                folderBrowserDialog.SelectedPath = !string.IsNullOrEmpty(this.SelectedPath) ? this.SelectedPath : this.InitialDirectory;
                folderBrowserDialog.ShowNewFolderButton = false;
                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                {
                    fileDialog.FileName = folderBrowserDialog.SelectedPath;
                    return true;
                }
                return false;
            }
        }
    
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {      
                try
                {
                    bool flag = false;
                    System.Reflection.Assembly assembly = Assembly.Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
                    Type typeIFileDialog = assembly.GetType("System.Windows.Forms.FileDialogNative").GetNestedType("IFileDialog", BindingFlags.NonPublic);
                    uint num = 0;
                    object dialog = InvokeMethod(fileDialog.GetType(), fileDialog, "CreateVistaDialog", null);
                    InvokeMethod(fileDialog.GetType(), fileDialog, "OnBeforeVistaDialog", new object[] { dialog });
                    uint options = (uint)InvokeMethod(typeof(System.Windows.Forms.FileDialog), fileDialog, "GetOptions", null) | (uint)0x20;
                    InvokeMethod(typeIFileDialog, dialog, "SetOptions", new object[] { options });
                    Type vistaDialogEventsType = assembly.GetType("System.Windows.Forms.FileDialog").GetNestedType("VistaDialogEvents", BindingFlags.NonPublic);
                    object pfde = Activator.CreateInstance(vistaDialogEventsType, fileDialog);
                    object[] parameters = new object[] { pfde, num };
                    InvokeMethod(typeIFileDialog, dialog, "Advise", parameters);
                    num = (uint)parameters[1];
                    try
                    {
                        int num2 = (int)InvokeMethod(typeIFileDialog, dialog, "Show", new object[] { hwndOwner });
                        flag = 0 == num2;
                    }
                    finally
                    {
                        InvokeMethod(typeIFileDialog, dialog, "Unadvise", new object[] { num });
                        GC.KeepAlive(pfde);
                    }
                    return flag;
                }
                catch
                {
                    return ShowOriginalBrowserDialog(hwndOwner);
                }
            }
            else
                return ShowOriginalBrowserDialog(hwndOwner);
        }
    
        public override void Reset()
        {
            fileDialog.Reset();
        }
    }
    '@
    $fbd = New-Object FolderBrowserModernDialog
    


    \_(ツ)_/

    Saturday, January 27, 2018 12:56 AM
  • thanks jrv. Appreciate it

    Saturday, January 27, 2018 1:02 AM