Click here to Skip to main content
15,886,199 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: PowerShell - FileSystemWatcher with GUI issue Pin
shreya313-Oct-19 2:17
shreya313-Oct-19 2:17 
QuestionForm appears hidden behind google maps in expanded mode. Pin
BibiStars6-Sep-19 4:11
BibiStars6-Sep-19 4:11 
AnswerRe: Form appears hidden behind google maps in expanded mode. Pin
dan!sh 6-Sep-19 4:44
professional dan!sh 6-Sep-19 4:44 
GeneralRe: Form appears hidden behind google maps in expanded mode. Pin
BibiStars7-Sep-19 3:28
BibiStars7-Sep-19 3:28 
GeneralRe: Form appears hidden behind google maps in expanded mode. Pin
Ryan Elias22-Oct-19 2:35
Ryan Elias22-Oct-19 2:35 
Questionsqlconnection Pin
Nithya MCA25-Aug-19 5:10
Nithya MCA25-Aug-19 5:10 
AnswerRe: sqlconnection Pin
Gerry Schmitz25-Aug-19 11:37
mveGerry Schmitz25-Aug-19 11:37 
QuestionOptimize Directory Parsing and TreeView Population Pin
Harley Burton13-Aug-19 3:16
Harley Burton13-Aug-19 3:16 
I need to parse what can potentially be a deep directory tree (Windows User Profile Directory), and populate a TreeView with the files and subdirectories. I have code that "works," but it's a little slow, and it hangs up the form more than I like.

Obviously, it takes a while to parse the profile tree, and I will eventually run that code in a separate thread, but another problem that I'm having is the TreeView displays the root node, collapsed, initially, and expanding that node holds the UI up again, and it takes 5 seconds or so to expand and become responsive again.

Just wondering if anyone could give me some pointers that may optimize the processes?

Thanks.

This is my Form1.cs code. Not much interesting outside of it.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Specialized;


namespace Migration_Wizard {
    public partial class Form1 : Form {
        bool debug_on = true;
        public enum LOG_LEVEL {
            debug = 0,
            info = 1,
            warning = 2,
            error = 3,
            access = 4
        }

        DirectoryInfo profiles_obj;

        StringCollection inaccessible = new StringCollection();
        StringCollection momentaries = new StringCollection(); // Files that existed earlier but don't exist during parse.
        StringCollection copy_failures = new StringCollection();


        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
        private const int WM_VSCROLL = 277;
        private const int SB_PAGEBOTTOM = 7;
        internal static void ScrollToBottom(RichTextBox richTextBox) {
            SendMessage(richTextBox.Handle, WM_VSCROLL, (IntPtr)SB_PAGEBOTTOM, IntPtr.Zero);
            richTextBox.SelectionStart = richTextBox.Text.Length;
        }

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            profiles_obj = new DirectoryInfo(@"C:\Users");
            DirectoryInfo[] profiles = profiles_obj.GetDirectories();
            //profileNameCbx.DataSource = profiles;
            foreach(DirectoryInfo profile in profiles) {
                if(
                    profile.Name != "All Users" &&
                    profile.Name != "Default" &&
                    profile.Name != "Default User" &&
                    profile.Name != "Public"
                    ) {
                    profileNameCbx.Items.Add(profile);
                }
            }
        }

        private void ProfileNameCbx_SelectedValueChanged(object sender, EventArgs e) {
            var profileDir_path = @"C:\Users\" + profileNameCbx.Text;
            if (Directory.Exists(profileDir_path)) {
                browserTV.Nodes.Clear();
                TreeNode root = browserTV.Nodes.Add(profileDir_path, profileNameCbx.Text, 0);
                ParseProfile(root);
            }
        }


        public void ParseProfile(TreeNode parent_node) {
            DirectoryInfo parent_di = new DirectoryInfo(parent_node.Name);
            FileInfo[] files = null;
            DirectoryInfo[] subDirs = null;
            try {
                files = parent_di.GetFiles("*.*").Where(file =>
                    (file.Attributes & FileAttributes.Hidden) == 0).ToArray();
            }
            catch(UnauthorizedAccessException e) {
                inaccessible.Add(parent_di.FullName);
                Log(e.Message, LOG_LEVEL.access);
            }
            catch (DirectoryNotFoundException e) {
                momentaries.Add(parent_di.FullName);
                Log(e.Message, LOG_LEVEL.warning);
            }
            catch (Exception e) {
                Log(e.Message, LOG_LEVEL.debug);
                // Throw this in the end.
            }

            if(files != null) {
                subDirs = parent_di.GetDirectories().Where(directory =>
                    (directory.Attributes & FileAttributes.Hidden) == 0).ToArray(); ;
                foreach(DirectoryInfo dir in subDirs) {
                    TreeNode this_node = parent_node.Nodes.Add(dir.FullName, dir.Name, 0);

                    // Recusive call for each subdirectory
                    ParseProfile(this_node);
                }

                foreach (FileInfo f in files) {
                    parent_node.Nodes.Add(f.FullName, f.Name, 1);
                }

                //browserTV.Refresh();
            }
        }

        public void Log(string msg, LOG_LEVEL lvl) {
            Color orig = consolRTB.SelectionColor;
            switch (lvl) {
                case LOG_LEVEL.debug:
                    if (debug_on) {
                        consolRTB.SelectionColor = Color.FromArgb(225, 226, 239);
                    }
                    break;
                case LOG_LEVEL.info:
                    consolRTB.SelectionColor = Color.FromArgb(170,246,131);
                    break;
                case LOG_LEVEL.warning:
                    consolRTB.SelectionColor = Color.FromArgb(255, 217, 125);
                    break;
                case LOG_LEVEL.error:
                    consolRTB.SelectionColor = Color.FromArgb(238, 96, 85);
                    break;
                case LOG_LEVEL.access:
                    consolRTB.SelectionColor = Color.FromArgb(145, 196, 242);
                    break;
            }
            consolRTB.AppendText(String.Concat("\n",msg,"\n"));
            consolRTB.SelectionColor = orig;
        }

        private void ConsolRTB_TextChanged(object sender, EventArgs e) {
            if (autoscrollChB.Checked) {
                ScrollToBottom(consolRTB);
            }
        }

        private void AutoscrollChB_CheckedChanged(object sender, EventArgs e) {
            if (autoscrollChB.Checked) {
                ScrollToBottom(consolRTB);
            }
        }
    }
}

AnswerRe: Optimize Directory Parsing and TreeView Population Pin
Richard Deeming13-Aug-19 3:51
mveRichard Deeming13-Aug-19 3:51 
GeneralRe: Optimize Directory Parsing and TreeView Population Pin
Harley Burton13-Aug-19 10:41
Harley Burton13-Aug-19 10:41 
GeneralRe: Optimize Directory Parsing and TreeView Population Pin
Richard Deeming13-Aug-19 22:35
mveRichard Deeming13-Aug-19 22:35 
GeneralRe: Optimize Directory Parsing and TreeView Population Pin
Harley Burton14-Aug-19 4:52
Harley Burton14-Aug-19 4:52 
QuestionWindows Service Installer Problem Pin
Kevin Marois7-Aug-19 6:14
professionalKevin Marois7-Aug-19 6:14 
AnswerRe: Windows Service Installer Problem Pin
Richard Andrew x647-Aug-19 11:47
professionalRichard Andrew x647-Aug-19 11:47 
GeneralRe: Windows Service Installer Problem Pin
Kevin Marois7-Aug-19 12:13
professionalKevin Marois7-Aug-19 12:13 
Question.Net Core web tiff vieawer Pin
Member 145516927-Aug-19 3:55
Member 145516927-Aug-19 3:55 
AnswerRe: .Net Core web tiff vieawer Pin
Richard MacCutchan7-Aug-19 4:14
mveRichard MacCutchan7-Aug-19 4:14 
GeneralRe: .Net Core web tiff vieawer Pin
Member 145516927-Aug-19 4:31
Member 145516927-Aug-19 4:31 
GeneralRe: .Net Core web tiff vieawer Pin
Richard MacCutchan7-Aug-19 5:55
mveRichard MacCutchan7-Aug-19 5:55 
AnswerRe: .Net Core web tiff vieawer Pin
Richard Deeming7-Aug-19 9:44
mveRichard Deeming7-Aug-19 9:44 
QuestionInstalling A Windows Service on Win 10 With Windows Defender Pin
Kevin Marois6-Aug-19 7:23
professionalKevin Marois6-Aug-19 7:23 
AnswerRe: Installing A Windows Service on Win 10 With Windows Defender Pin
Richard Andrew x646-Aug-19 12:13
professionalRichard Andrew x646-Aug-19 12:13 
SuggestionRe: Installing A Windows Service on Win 10 With Windows Defender Pin
Richard Deeming7-Aug-19 9:40
mveRichard Deeming7-Aug-19 9:40 
QuestionHow to crystal report Short by Month.Year Pin
NSE India5-Aug-19 22:08
NSE India5-Aug-19 22:08 
AnswerRe: How to crystal report Short by Month.Year Pin
Richard Andrew x646-Aug-19 12:23
professionalRichard Andrew x646-Aug-19 12:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.