C# を使用して "ファイル エクスプローラー" ソリューションを作成しています。これが私のコードです:
namespace NIXplorer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TreeNode root = new TreeNode("Desktop");
TreeNode doc = new TreeNode("My Documents");
TreeNode comp = new TreeNode("My Computer ");
TreeNode drivenode;
TreeNode filenode;
DirectoryInfo dir;
string path = "";
private void Form1_Load(object sender, EventArgs e)
{
listView1.LabelEdit = true;
listView1.FullRowSelect = true;
listView1.Sorting = SortOrder.Ascending;
treeView1.Nodes.Add(root);
root.ImageIndex = 0;
doc.ImageIndex = 1;
comp.ImageIndex = 2;
treeView1.Nodes.Add(doc);
treeView1.Nodes.Add(comp);
GetDrives();
}
private void GetDrives()
{
DriveInfo[] drive = DriveInfo.GetDrives();
foreach (DriveInfo d in drive)
{
drivenode = new TreeNode(d.Name);
dir = d.RootDirectory;
comp.Nodes.Add(drivenode);
//drivenode.ImageIndex = 3;
switch (d.DriveType)
{
case DriveType.CDRom:
drivenode.ImageIndex = 5;
break;
//case DriveType.Fixed:
// drivenode.ImageIndex = 1;
// break;
case DriveType.Removable:
drivenode.ImageIndex = 8;
break;
//case DriveType.NoRootDirectory:
// drivenode.ImageIndex = 5;
// break;
case DriveType.Network:
drivenode.ImageIndex = 6;
break;
default:
drivenode.ImageIndex = 7;
break;
}
getFilesAndDir(drivenode, dir);
}
}
private void getFilesAndDir(TreeNode node, DirectoryInfo dirname)
{
try
{
foreach (FileInfo fi in dirname.GetFiles())
{
filenode = new TreeNode(fi.Name);
filenode.Name = fi.FullName;
//filenode.ImageIndex = 5;
getFileExtension(filenode.Name);
node.Nodes.Add(filenode);
}
try
{
foreach (DirectoryInfo di in dirname.GetDirectories())
{
TreeNode dirnode = new TreeNode(di.Name);
dirnode.ImageIndex = 4;
dirnode.Name = di.FullName;
node.Nodes.Add(dirnode);
getFilesAndDir(dirnode, di); //Recursive Functioning
}
}
catch (Exception e1)
{
}
}
catch (Exception e1)
{
}
}
private void getFileExtension(string filename)
{
switch (Path.GetExtension(filename))
{
case ".txt":
filenode.ImageIndex = 17;
break;
case ".rtf":
filenode.ImageIndex = 16;
break;
case ".doc":
case ".docx":
filenode.ImageIndex = 9;
break;
case ".html":
case ".htm":
filenode.ImageIndex = 13;
break;
case ".rar":
case ".zip":
filenode.ImageIndex = 10;
break;
case ".exe":
filenode.ImageIndex = 12;
break;
case ".mp3":
case ".wma":
case ".flac":
case ".m4a":
filenode.ImageIndex = 11;
break;
case ".mp4":
case ".flv":
case ".mkv":
case ".webm":
filenode.ImageIndex = 18;
break;
case ".ico":
case ".png":
case ".jpg":
case ".jpeg":
case ".psd":
filenode.ImageIndex = 14;
break;
default:
filenode.ImageIndex = 15;
break;
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
try
{
listView1.Items.Clear();
TreeNode selectednode = e.Node;
treeView1.SelectedNode.ImageIndex = e.Node.ImageIndex;
selectednode.Expand();
comboBox1.Text = selectednode.FullPath;
if (selectednode.Nodes.Count > 0)
{
foreach (TreeNode n in selectednode.Nodes)
{
ListViewItem lst = new ListViewItem(n.Text, n.ImageIndex);
lst.Name = n.FullPath.Substring(13);
//MessageBox.Show("List Node : " + lst.Name);
listView1.Items.Add(lst);
}
}
else
{
listView1.Items.Add(selectednode.FullPath, selectednode.Text, selectednode.ImageIndex);
}
}
catch (Exception e1)
{
}
}
}
}
私はVS2012でこのソリューションを構築していますが、このソリューションをデバッグするたびに、開始するのに1〜2分以上かかるため、このアルゴリズムは最良の選択ではないようです...これは本当に悪いです...ソリューションまたはアルゴリズム? それは素晴らしいことです。前もって感謝します !