Hallo zusammen, habe es mittlerweile mit Umwegen und Zwischenvariablen geschafft mein Problem zu erreichen
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Runtime.InteropServices;
using System.Collections;
using System.Data.SqlClient;
using System.Xml;
namespace WindowsFormsApplication1
{
public partial class Form4 : Form
{
Hashtable treenodes = new Hashtable();
ArrayList openNodes = new ArrayList();
public Form4(string Src)
{
InitializeComponent();
drives_Src(Src);
checkNodes(Src);
}
private void drives_Src(string source)
{
TreeNode node = new TreeNode();
foreach (string drive in Directory.GetLogicalDrives())
{
try
{
string NodeSrc = "";
Boolean fehler = true;
string NodeSource = "";
string[] sources = source.Split('|');
foreach (string strSource in sources)
{
string[] strNode = strSource.Split('\\');
string NodeSrctemp = strNode[0] + "\\";
if (drive == NodeSrctemp)
{
if (NodeSrctemp == NodeSrc)
{
NodeSource = NodeSource + "|" + strSource;
fehler = false;
}
else
{
NodeSrc = NodeSrctemp;
NodeSource = strSource;
fehler = false;
}
}
}
if (fehler == false)
{
eNode_Src(NodeSource,drive);
}
else
{
DriveType drive_type = GetDriveType(drive);
string[] strdrive = drive.Split('\\');
node = treeView1.Nodes.Add(drive);
switch (drive_type.ToString())
{
case "DRIVE_FIXED":
node.ImageKey = "drive";
node.SelectedImageKey = "drive";
break;
case "DRIVE_REMOTE":
node.ImageKey = "network";
node.SelectedImageKey = "network";
break;
case "DRIVE_CDROM":
node.ImageKey = "cdrom";
node.SelectedImageKey = "cdrom";
break;
default:
node.ImageKey = "drive";
node.SelectedImageKey = "drive";
break;
}
//Dummynodes einfügen
TreeNode dummy = new TreeNode();
dummy = node.Nodes.Add("");
dummy.ImageKey = "folder";
}
}
catch { }
}
} //Ende drives_Src()
public enum DriveType
{
DRIVE_UNKNOWN = 0,
DRIVE_NO_ROOT_DIR = 1,
DRIVE_REMOVABLE = 2,
DRIVE_FIXED = 3,
DRIVE_REMOTE = 4,
DRIVE_CDROM = 5,
DRIVE_RAMDISK = 6
} //Ende DriveType
// Um heraus zu finden um was für ne laufwerksart es sich handelt...
[DllImport("KERNEL32", EntryPoint = "GetDriveType")]
public static extern DriveType GetDriveType(string sDrive);
int i = 0;
// TreeView Quelle
private void eNode_Src(string SrcNodes, string drive)
{
TreeNode parentNode = null;
string[] Sources = SrcNodes.Split('|');
foreach (string Source in Sources)
{
TreeNode tmp = null;
string SrcPart = "";
string[] splittetSrc = Source.Split('\\');
foreach (string SrcParted in splittetSrc)
{
if (SrcPart == "")
{
SrcPart = SrcParted + "\\";
Boolean vorhanden = false;
foreach (DictionaryEntry node in treenodes)
{
if (node.Value.ToString() == SrcPart)
{
vorhanden = true;
parentNode = (TreeNode)node.Key;
}
}
if (vorhanden != true)
{
parentNode = treeView1.Nodes.Add(drive);
parentNode.Expand();
treenodes.Add(parentNode, drive);
}
}
else
{
SrcPart += SrcParted + "\\";
parentNode = tmp;
}
foreach (string dir in Directory.GetDirectories(SrcPart))
{
try
{
Boolean vorhanden = false;
TreeNode dir_t = new TreeNode();
TreeNode test = new TreeNode();
string[] ordnername = dir.Split('\\');
i = ordnername.Length - 1;
foreach (DictionaryEntry node in treenodes)
{
if (node.Value.ToString() == dir)
{
vorhanden = true;
if (Source.Contains(dir) && dir != Source)
{
parentNode.Nodes.Remove((TreeNode)node.Key);
dir_t = parentNode.Nodes.Add(ordnername[i].ToString());
this.treeView1.Sort();
dir_t.Expand();
dir_t.ImageKey = "folder";
dir_t.SelectedImageKey = "folder";
tmp = dir_t;
}
}
}
if (vorhanden != true)
{
dir_t = parentNode.Nodes.Add(ordnername[i].ToString());
dir_t.Expand();
dir_t.ImageKey = "folder";
dir_t.SelectedImageKey = "folder";
if (Source.Contains(dir))
{
tmp = dir_t;
}
treenodes.Add(dir_t, dir);
}
}
catch (Exception ex)
{
MessageBox.Show("Catch: " + ex.Message + " dir: " + dir);
}
}
}
}
}//Ende treeView1 (Quelle)
private void checkNodes(String SrcNodes)
{
string[] tmpSources = SrcNodes.Split('|');
foreach (string Source in tmpSources)
{
//String Source vorbereiten
string strTemp = "";
string[] strSource = Source.Split('\\');
foreach (string tmpStr in strSource)
{
if (strTemp == "")
{
strTemp = tmpStr + "\\";
}
else
{
strTemp += "\\" + tmpStr;
}
}
// Hier werden die Nodes gecheckt
CallRecursive(treeView1, strTemp);
}
}
private void PrintRecursive(TreeNode treeNode, String Source)
{
if (treeNode.Text != "")
{
if (treeNode.FullPath == Source)
{
treeNode.Checked = true;
}
}
foreach (TreeNode tn in treeNode.Nodes)
{
PrintRecursive(tn, Source);
}
}
// Call the procedure using the TreeView.
private void CallRecursive(TreeView treeView, string Source)
{
TreeNodeCollection nodes = treeView.Nodes;
foreach (TreeNode n in nodes)
{
PrintRecursive(n, Source);
}
}
}
}
|