コード例 #1
0
ファイル: XwShellTree.cs プロジェクト: maxsnts/XwMaxLib
        //***********************************************************************************************
        public void Load()
        {
            BeginUpdate();
            Nodes.Clear();
            desktop            = new TreeNode(System.Environment.SpecialFolder.Desktop.ToString());
            desktop.Tag        = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            desktop.ImageIndex = desktop.SelectedImageIndex = ShellImageList.GetSpecialFolderImageIndex(ShellAPI.CSIDL.DESKTOP);
            Nodes.Add(desktop);
            SelectedNode = desktop;

            TreeNode documents = new TreeNode(System.Environment.SpecialFolder.MyDocuments.ToString());

            documents.Tag        = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            documents.ImageIndex = documents.SelectedImageIndex = ShellImageList.GetFileImageIndex(documents.Tag.ToString(), FileAttributes.Directory | FileAttributes.System);
            desktop.Nodes.Add(documents);
            LoadNode(documents, true);

            computer            = new TreeNode(System.Environment.SpecialFolder.MyComputer.ToString());
            computer.Tag        = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
            computer.ImageIndex = computer.SelectedImageIndex = ShellImageList.GetSpecialFolderImageIndex(ShellAPI.CSIDL.DRIVES);
            desktop.Nodes.Add(computer);

            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                if (string.Compare(drive.Name, "A:\\", true) == 0 && drive.DriveType == DriveType.Removable)
                {
                    continue;
                }

                if (string.Compare(drive.Name, "B:\\", true) == 0 && drive.DriveType == DriveType.Removable)
                {
                    continue;
                }

                string   name    = String.Format("{0} ({1})", (drive.IsReady)?drive.VolumeLabel:drive.DriveType.ToString(), drive.Name.Replace("\\", ""));
                TreeNode drvNode = new TreeNode(name);
                drvNode.Tag        = drive.RootDirectory.FullName;
                drvNode.ImageIndex = drvNode.SelectedImageIndex = ShellImageList.GetFileImageIndex(drive.Name, FileAttributes.Device);
                computer.Nodes.Add(drvNode);

                if (drive.IsReady)
                {
                    DirectoryInfo   checkD = new DirectoryInfo(drive.RootDirectory.FullName);
                    DirectoryInfo[] check  = checkD.GetDirectories();
                    if (check.Length > 0)
                    {
                        TreeNode dummy = new TreeNode(string.Empty);
                        drvNode.Nodes.Add(dummy);
                    }
                }
            }

            LoadNode(desktop, true);
            desktop.Expand();
            computer.Expand();
            EndUpdate();

            RegisterForDeviceChange(true);
        }
コード例 #2
0
        //********************************************************************************************
        public void RealLoadList(string path, bool skipCheckLink)
        {
            BeginUpdate();
            Items.Clear();

            if (skipCheckLink)
            {
                form.SkipCheckLink = true;
            }

            if (path == string.Empty && form.LocalTree.SelectedNode != null) //my computer
            {
                foreach (TreeNode n in form.LocalTree.SelectedNode.Nodes)
                {
                    ListViewItem i = Items.Add(n.Text, n.ImageIndex);
                    i.Tag = new DiskItem(true, false, n.Tag.ToString(), "");
                }
            }
            else
            {
                try
                {
                    if (path.Contains("\\\\") && !path.Replace("\\\\", "").Contains("\\"))
                    {
                        try
                        {
                            //New instance of the management path so we can use its properties
                            ManagementPath mpath = new ManagementPath();
                            //Set the Servername
                            mpath.Server = path.Replace("\\\\", "");
                            //Set the WMI namespace
                            mpath.NamespacePath = @"root\cimv2";
                            //Here we are using the default connections but we can also use different.
                            //Username and password if we need to.
                            ConnectionOptions oConn = new ConnectionOptions();
                            //Set the Scope ...Computername and WMI namespace
                            ManagementScope scope = new ManagementScope(path, oConn);
                            //Set the WMI Class
                            mpath.RelativePath = "Win32_Share";

                            //Set shares to null
                            ManagementClass Shares = null;
                            //Here we are connecting using the Servername and WMI Namespace/Class
                            using (Shares = new ManagementClass(scope, mpath, null))
                            {
                                //Return a collection of Shares here
                                ManagementObjectCollection moc = Shares.GetInstances();

                                //Go thru each share and display its name property in the list box.
                                foreach (ManagementObject mo in moc)
                                {
                                    string       share = mo["Name"].ToString();
                                    ListViewItem item  = new ListViewItem(share, 0);
                                    item.Tag        = new DiskItem(true, false, Path.Combine(path, share), share);
                                    item.ImageIndex = ShellImageList.GetSpecialFolderImageIndex((ShellAPI.CSIDL) 49);
                                    Items.Add(item);
                                }
                            }
                        }
                        catch (Exception) //catch any exceptions we might have .
                        {
                            MessageBox.Show("Unable to return sharenames . Please make share Servername is correct.");
                        }
                    }
                    else
                    {
                        DirectoryInfo nodeDirInfo = new DirectoryInfo(path);
                        ListViewItem.ListViewSubItem[] subItems;
                        ListViewItem item = null;

                        DirectoryInfo[] dirs = nodeDirInfo.GetDirectories();

                        ListViewItem item1 = Items.Add(".", ShellImageList.GetFileImageIndex(".", FileAttributes.Directory));
                        item1.Tag       = new DiskItem(true, false, ".", ".");
                        item1.BackColor = Color.FromArgb(240, 240, 240);
                        ListViewItem item2 = Items.Add("..", ShellImageList.GetFileImageIndex("Folder", FileAttributes.Directory));
                        item2.Tag       = new DiskItem(true, false, "..", "..");
                        item2.BackColor = Color.FromArgb(230, 230, 230);

                        foreach (DirectoryInfo dir in dirs)
                        {
                            if ((dir.Attributes & FileAttributes.Hidden) != 0)
                            {
                                continue;
                            }

                            item     = new ListViewItem(dir.Name, 0);
                            subItems = new ListViewItem.ListViewSubItem[]
                            {
                                new ListViewItem.ListViewSubItem(item, " - - - "),
                                new ListViewItem.ListViewSubItem(item,
                                                                 dir.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"))
                            };

                            item.SubItems.AddRange(subItems);
                            item.Tag        = new DiskItem(true, false, dir.FullName, dir.Name);
                            item.ImageIndex = ShellImageList.GetFileImageIndex(dir.FullName, dir.Attributes);
                            Items.Add(item);
                        }

                        foreach (FileInfo file in nodeDirInfo.GetFiles())
                        {
                            if ((file.Attributes & FileAttributes.Hidden) != 0)
                            {
                                continue;
                            }

                            item     = new ListViewItem(file.Name, 1);
                            subItems = new ListViewItem.ListViewSubItem[]
                            {
                                new ListViewItem.ListViewSubItem(item, String.Format("{0:0,0}", file.Length)),
                                new ListViewItem.ListViewSubItem(item,
                                                                 file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"))
                            };

                            item.SubItems.AddRange(subItems);
                            item.Tag = new DiskItem(false, false, file.FullName, file.Name, file.Length);
                            //item.ImageIndex = ShellImageList.GetFileImageIndex(file.FullName, file.Attributes);
                            item.ImageIndex = ShellImageList.GetFileImageIndex(file.FullName, file.Attributes);
                            Items.Add(item);
                        }

                        if (!string.IsNullOrEmpty(path))
                        {
                            fileSystemWatcher.Path = path;
                            fileSystemWatcher.EnableRaisingEvents = true;
                        }
                        else
                        {
                            fileSystemWatcher.EnableRaisingEvents = false;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Path", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            EndUpdate();
            form.statusLocalCount.Text = String.Format("{0} Items", (Items.Count > 2)?Items.Count - 2:0);

            CurrentDirectory    = path;
            form.LocalPath.Text = path;
            CheckPin();
        }