Created
March 5, 2023 17:21
-
-
Save pmache/69e868dc92a31d24d3bbca9282e333f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.IO; | |
using System.Net; | |
using System.Runtime.InteropServices; | |
using System.Windows.Forms; | |
using Microsoft.Win32; | |
using System.Diagnostics; | |
namespace FileManager | |
{ | |
public partial class MainForm : Form | |
{ | |
private ImageList imageList; | |
private ListView listView; | |
private TreeView treeView1; | |
private SplitContainer splitContainer1; | |
private WebBrowser webBrowser1; | |
private TextBox textBox1; | |
private Button button1; | |
private Button button2; | |
private MenuStrip menuStrip1; | |
private ContextMenuStrip contextMenuStrip1; | |
private System.ComponentModel.IContainer components; | |
private TreeView treeView; | |
static class Program | |
{ | |
[STAThread] | |
static void Main() | |
{ | |
Application.EnableVisualStyles(); | |
Application.SetCompatibleTextRenderingDefault(false); | |
Application.Run(new MainForm()); | |
} | |
} | |
public MainForm() | |
{ | |
InitializeComponent(); | |
// Initialize the file system tree view | |
treeView = new TreeView(); | |
treeView.Dock = DockStyle.Left; | |
treeView.AfterSelect += TreeView_AfterSelect; | |
Controls.Add(treeView); | |
// Initialize the file list view | |
listView = new ListView(); | |
listView.Dock = DockStyle.Fill; | |
listView.View = View.Details; | |
listView.FullRowSelect = true; | |
listView.GridLines = true; | |
listView.Columns.Add("Name", 250); | |
listView.Columns.Add("Size", 100); | |
listView.Columns.Add("Type", 100); | |
listView.Columns.Add("Modified", 150); | |
Controls.Add(listView); | |
// Initialize the image list with system icons | |
imageList = new ImageList(); | |
imageList.ColorDepth = ColorDepth.Depth32Bit; | |
imageList.ImageSize = new Size(16, 16); | |
imageList.Images.Add(GetIcon("shell32.dll", 3)); // Desktop | |
imageList.Images.Add(GetIcon("shell32.dll", 4)); // My Computer | |
imageList.Images.Add(GetIcon("shell32.dll", 9)); // Drive | |
imageList.Images.Add(GetIcon("shell32.dll", 15)); // Folder | |
listView.SmallImageList = imageList; | |
// Populate the file system tree view with drives | |
foreach (DriveInfo drive in DriveInfo.GetDrives()) | |
{ | |
if (drive.IsReady) | |
{ | |
TreeNode driveNode = new TreeNode(drive.Name, 2, 2); | |
driveNode.Tag = drive.RootDirectory.FullName; | |
treeView.Nodes.Add(driveNode); | |
driveNode.Nodes.Add(new TreeNode()); // dummy node | |
} | |
} | |
} | |
private void TreeView_AfterSelect(object sender, TreeViewEventArgs e) | |
{ | |
string path = e.Node.Tag as string; | |
if (path == null) | |
return; | |
try | |
{ | |
// Clear the list view | |
listView.Items.Clear(); | |
// Populate the list view with files and folders | |
DirectoryInfo dir = new DirectoryInfo(path); | |
foreach (DirectoryInfo subdir in dir.GetDirectories()) | |
{ | |
ListViewItem item = new ListViewItem(subdir.Name, 3); | |
item.SubItems.Add(""); | |
item.SubItems.Add("Folder"); | |
item.SubItems.Add(subdir.LastWriteTime.ToString()); | |
item.Tag = subdir.FullName; | |
listView.Items.Add(item); | |
} | |
foreach (FileInfo file in dir.GetFiles()) | |
{ | |
ListViewItem item = new ListViewItem(file.Name, GetImageIndex(file.FullName)); | |
item.SubItems.Add(file.Length.ToString("#,##0")); | |
item.SubItems.Add(file.Extension.ToUpper().Substring(1)); | |
item.SubItems.Add(file.LastWriteTime.ToString()); | |
item.Tag = file.FullName; | |
listView.Items.Add(item); | |
} | |
// Resize the list view columns | |
listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); | |
} | |
} | |
private int GetImageIndex(string path) | |
{ | |
SHFILEINFO shfi = new SHFILEINFO(); | |
uint flags = SHGFI_SYSICONINDEX | SHGFI_SMALLICON; | |
IntPtr hImg = SHGetFileInfo(path, 0, ref shfi, (uint)Marshal.SizeOf(shfi), flags); | |
if (hImg != IntPtr.Zero) | |
{ | |
return shfi.iIcon; | |
} | |
else | |
{ | |
return -1; | |
} | |
} | |
private Icon GetIcon(string fileName, int index) | |
{ | |
SHFILEINFO shfi = new SHFILEINFO(); | |
uint flags = SHGFI_SYSICONINDEX | SHGFI_SMALLICON; | |
SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), flags); | |
IntPtr hIcon = ImageList_GetIcon(imageList.Handle, shfi.iIcon, ILD_TRANSPARENT); | |
return Icon.FromHandle(hIcon); | |
} | |
private void MainForm_Load(object sender, EventArgs e) | |
{ | |
// Set the window title | |
RegistryKey key = Registry.ClassesRoot.OpenSubKey("*\\shell\\open\\command"); | |
if (key != null) | |
{ | |
string command = key.GetValue("").ToString(); | |
int start = command.IndexOf('"', 0) + 1; | |
int end = command.IndexOf('"', start); | |
string appName = command.Substring(start, end - start); | |
Text = Path.GetFileNameWithoutExtension(appName); | |
key.Close(); | |
} | |
} | |
private void listView_MouseDoubleClick(object sender, MouseEventArgs e) | |
{ | |
if (listView.SelectedItems.Count > 0) | |
{ | |
string path = listView.SelectedItems[0].Tag as string; | |
if (path != null) | |
{ | |
try | |
{ | |
if (Directory.Exists(path)) | |
{ | |
// Expand the tree view node | |
TreeNode node = FindNodeByTag(treeView.Nodes, path); | |
if (node != null) | |
{ | |
node.Expand(); | |
treeView.SelectedNode = node; | |
} | |
} | |
else if (File.Exists(path)) | |
{ | |
// Open the file with the default program | |
Process.Start(path); | |
} | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); | |
} | |
} | |
} | |
} | |
private TreeNode FindNodeByTag(TreeNodeCollection nodes, string tag) | |
{ | |
foreach (TreeNode node in nodes) | |
{ | |
if (node.Tag != null && node.Tag.ToString() == tag) | |
return node; | |
else | |
{ | |
TreeNode subNode = FindNodeByTag(node.Nodes, tag); | |
if (subNode != null) | |
return subNode; | |
} | |
} | |
return null; | |
} | |
private const uint SHGFI_ICON = 0x000000100; | |
private const uint SHGFI_SMALLICON = 0x000000001; | |
private const uint SHGFI_SYSICONINDEX = 0x000004000; | |
[DllImport("shell32.dll", CharSet = CharSet.Unicode)] | |
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); | |
[DllImport("comctl32.dll")] | |
private static extern IntPtr ImageList_GetIcon(IntPtr himl, int i, uint flags); | |
[StructLayout(LayoutKind.Sequential)] | |
private struct SHFILEINFO | |
{ | |
public IntPtr hIcon; | |
public int iIcon; | |
public uint dwAttributes; | |
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] | |
public string szDisplayName; | |
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] | |
public string szTypeName; | |
} | |
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) | |
{ | |
// Save the current directory and view mode to settings | |
Properties.Settings.Default.currentDirectory = currentDirectory; | |
Properties.Settings.Default.viewMode = (int)viewMode; | |
Properties.Settings.Default.Save(); | |
} | |
private void listView_KeyDown(object sender, KeyEventArgs e) | |
{ | |
if (e.KeyCode == Keys.Enter) | |
{ | |
// Open the selected file or directory | |
listView_MouseDoubleClick(null, null); | |
} | |
else if (e.KeyCode == Keys.Back) | |
{ | |
// Go up one level | |
GoUpOneLevel(); | |
} | |
} | |
private void treeView_KeyDown(object sender, KeyEventArgs e) | |
{ | |
if (e.KeyCode == Keys.Enter) | |
{ | |
// Expand or collapse the selected node | |
treeView.SelectedNode.Toggle(); | |
} | |
else if (e.KeyCode == Keys.Back) | |
{ | |
// Go up one level | |
GoUpOneLevel(); | |
} | |
} | |
private void GoUpOneLevel() | |
{ | |
string parentDirectory = Path.GetDirectoryName(currentDirectory); | |
if (parentDirectory != null) | |
{ | |
SetCurrentDirectory(parentDirectory); | |
} | |
} | |
private void SetCurrentDirectory(string parentDirectory) | |
{ | |
throw new NotImplementedException(); | |
} | |
private void listView_DragDrop(object sender, DragEventArgs e) | |
{ | |
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); | |
if (files != null) | |
{ | |
foreach (string file in files) | |
{ | |
if (Directory.Exists(file)) | |
{ | |
// Copy the directory | |
string destDir = Path.Combine(currentDirectory, Path.GetFileName(file)); | |
CopyDirectory(file, destDir); | |
} | |
else if (File.Exists(file)) | |
{ | |
// Copy the file | |
string destFile = Path.Combine(currentDirectory, Path.GetFileName(file)); | |
File.Copy(file, destFile); | |
} | |
} | |
PopulateListView(currentDirectory); | |
} | |
} | |
private void PopulateListView(string currentDirectory) | |
{ | |
throw new NotImplementedException(); | |
} | |
private void CopyDirectory(string file, string destDir) | |
{ | |
throw new NotImplementedException(); | |
} | |
private void listView_DragEnter(object sender, DragEventArgs e) | |
{ | |
if (e.Data.GetDataPresent(DataFormats.FileDrop)) | |
{ | |
e.Effect = DragDropEffects.Copy; | |
} | |
else | |
{ | |
e.Effect = DragDropEffects.None; | |
} | |
} | |
private void treeView_AfterSelect(object sender, TreeViewEventArgs e) | |
{ | |
SetCurrentDirectory(e.Node.Tag as string); | |
} | |
/*private void viewModeToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e) | |
{ | |
switch (viewModeToolStripComboBox.SelectedIndex) | |
{ | |
case 0: | |
viewMode = ViewMode.Details; | |
break; | |
case 1: | |
viewMode = ViewMode.LargeIcons; | |
break; | |
case 2: | |
viewMode = ViewMode.SmallIcons; | |
break; | |
case 3: | |
viewMode = ViewMode.List; | |
break; | |
case 4: | |
viewMode = ViewMode.Tile; | |
break; | |
} | |
PopulateListView(currentDirectory); | |
}*/ | |
private void aboutToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
AboutForm aboutForm = new AboutForm(); | |
aboutForm.ShowDialog(); | |
} | |
private void InitializeComponent() | |
{ | |
this.components = new System.ComponentModel.Container(); | |
this.treeView1 = new System.Windows.Forms.TreeView(); | |
this.splitContainer1 = new System.Windows.Forms.SplitContainer(); | |
this.textBox1 = new System.Windows.Forms.TextBox(); | |
this.button1 = new System.Windows.Forms.Button(); | |
this.button2 = new System.Windows.Forms.Button(); | |
this.menuStrip1 = new System.Windows.Forms.MenuStrip(); | |
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); | |
this.webBrowser1 = new System.Windows.Forms.WebBrowser(); | |
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); | |
this.splitContainer1.Panel1.SuspendLayout(); | |
this.splitContainer1.Panel2.SuspendLayout(); | |
this.splitContainer1.SuspendLayout(); | |
this.SuspendLayout(); | |
// | |
// treeView1 | |
// | |
this.treeView1.Location = new System.Drawing.Point(0, 0); | |
this.treeView1.Name = "treeView1"; | |
this.treeView1.Size = new System.Drawing.Size(181, 264); | |
this.treeView1.TabIndex = 0; | |
// | |
// splitContainer1 | |
// | |
this.splitContainer1.Location = new System.Drawing.Point(0, 55); | |
this.splitContainer1.Name = "splitContainer1"; | |
// | |
// splitContainer1.Panel1 | |
// | |
this.splitContainer1.Panel1.Controls.Add(this.treeView1); | |
// | |
// splitContainer1.Panel2 | |
// | |
this.splitContainer1.Panel2.Controls.Add(this.webBrowser1); | |
this.splitContainer1.Size = new System.Drawing.Size(697, 270); | |
this.splitContainer1.SplitterDistance = 184; | |
this.splitContainer1.TabIndex = 1; | |
// | |
// textBox1 | |
// | |
this.textBox1.Location = new System.Drawing.Point(67, 28); | |
this.textBox1.Name = "textBox1"; | |
this.textBox1.Size = new System.Drawing.Size(579, 20); | |
this.textBox1.TabIndex = 2; | |
// | |
// button1 | |
// | |
this.button1.Location = new System.Drawing.Point(3, 27); | |
this.button1.Name = "button1"; | |
this.button1.Size = new System.Drawing.Size(27, 23); | |
this.button1.TabIndex = 3; | |
this.button1.Text = "button1"; | |
this.button1.UseVisualStyleBackColor = true; | |
// | |
// button2 | |
// | |
this.button2.Location = new System.Drawing.Point(36, 26); | |
this.button2.Name = "button2"; | |
this.button2.Size = new System.Drawing.Size(25, 23); | |
this.button2.TabIndex = 4; | |
this.button2.Text = "button2"; | |
this.button2.UseVisualStyleBackColor = true; | |
// | |
// menuStrip1 | |
// | |
this.menuStrip1.Location = new System.Drawing.Point(0, 0); | |
this.menuStrip1.Name = "menuStrip1"; | |
this.menuStrip1.Size = new System.Drawing.Size(694, 24); | |
this.menuStrip1.TabIndex = 5; | |
this.menuStrip1.Text = "menuStrip1"; | |
// | |
// contextMenuStrip1 | |
// | |
this.contextMenuStrip1.Name = "contextMenuStrip1"; | |
this.contextMenuStrip1.Size = new System.Drawing.Size(61, 4); | |
// | |
// webBrowser1 | |
// | |
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill; | |
this.webBrowser1.Location = new System.Drawing.Point(0, 0); | |
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20); | |
this.webBrowser1.Name = "webBrowser1"; | |
this.webBrowser1.Size = new System.Drawing.Size(509, 270); | |
this.webBrowser1.TabIndex = 0; | |
// | |
// MainForm | |
// | |
this.ClientSize = new System.Drawing.Size(694, 319); | |
this.Controls.Add(this.button2); | |
this.Controls.Add(this.button1); | |
this.Controls.Add(this.textBox1); | |
this.Controls.Add(this.splitContainer1); | |
this.Controls.Add(this.menuStrip1); | |
this.MainMenuStrip = this.menuStrip1; | |
this.Name = "MainForm"; | |
this.splitContainer1.Panel1.ResumeLayout(false); | |
this.splitContainer1.Panel2.ResumeLayout(false); | |
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit(); | |
this.splitContainer1.ResumeLayout(false); | |
this.ResumeLayout(false); | |
this.PerformLayout(); | |
} | |
public uint ILD_TRANSPARENT { get; set; } | |
public string currentDirectory { get; set; } | |
public int viewMode { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment