📄 form1.cs
字号:
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
}
else
{
int iTotal=(int)SHGetFileInfo(FileName,0,ref fi,100, 257);
try
{
if(iTotal >0)
{
Icon ic=Icon.FromHandle(fi.hIcon);
imageList.Images.Add(ic);
//return ic;
}
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
}
// return null;
}
//*************************************************************************************
public void GetDrive()
{
treeView1.ImageList=TreeImageList;
treeView1.BeginUpdate();
treeView1.Nodes.Clear();
TreeNode RootNode=new TreeNode("我的电脑",0,0);
treeView1.Nodes.Add(RootNode);
int iImageIndex=2; int iSelectedIndex=2;
string[] astrDrives = Directory.GetLogicalDrives();
foreach (string str in astrDrives)
{
if(str == "A:\\")
{ iImageIndex=1; iSelectedIndex=1;}
else if(str == "G:\\")
{ iImageIndex=3; iSelectedIndex=3;}
else
{ iImageIndex=2; iSelectedIndex=2;}
TreeNode tnDrive = new TreeNode(str, iImageIndex,iSelectedIndex);
treeView1.Nodes[0].Nodes.Add(tnDrive);
AddDirectories(tnDrive);
if(str == "C:\\")
{ treeView1.SelectedNode=tnDrive;}
}
treeView1.EndUpdate();
}
//*************************************************************************************
void AddDirectories(TreeNode tn)
{
tn.Nodes.Clear();
string strPath=tn.FullPath;
strPath=strPath.Remove(0,5);
//获得当前目录
DirectoryInfo dirinfo = new DirectoryInfo(strPath);
DirectoryInfo[] adirinfo;
try
{
adirinfo = dirinfo.GetDirectories();
}
catch
{ return;}
int iImageIndex=4; int iSelectedIndex=5;
foreach (DirectoryInfo di in adirinfo)
{
if(di.Name=="RECYCLER"||di.Name=="RECYCLED"||di.Name=="Recycled")
{iImageIndex=6; iSelectedIndex=6;}
else
{iImageIndex=4; iSelectedIndex=5;}
TreeNode tnDir = new TreeNode(di.Name, iImageIndex, iSelectedIndex);
tn.Nodes.Add(tnDir);
}
/*
//获得当前目录下的所有文件
FileInfo[] dirFiles;
dirFiles=dirinfo.GetFiles();
int iCount=7;
foreach (FileInfo fi in dirFiles)
{
//得到每个文件的图标
string str=fi.FullName;
try
{
SetIcon(TreeImageList,str,false);
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
TreeNode tnDir = new TreeNode(fi.Name, iCount, iCount);
tn.Nodes.Add(tnDir);
iCount++;
}
*/
}
//*************************************************************************************
//在大图标、小图标、详细列表切换时调用
protected virtual void ListViewAB(int iii)
{
if(iii==1)
{
LisrimageList2.ImageSize=new Size(32,32);
listView1.LargeImageList=LisrimageList2;
if(listView1.View==View.Details||listView1.View==View.SmallIcon)
{listView1.View=View.LargeIcon;}
}
else if(iii==2)
{
if(listView1.View==View.Details||listView1.View==View.LargeIcon)
{listView1.View=View.SmallIcon;}
}
else
{
if(listView1.View==View.LargeIcon||listView1.View==View.SmallIcon)
{listView1.View=View.Details;}
}
}
//*************************************************************************************
private void treeView1_BeforeExpand_1(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
{
//base.OnBeforeExpand(e);
treeView1.BeginUpdate();
foreach (TreeNode tn in e.Node.Nodes)
{ AddDirectories(tn);}
treeView1.EndUpdate();
}
//*************************************************************************************
protected virtual void InitList(TreeNode tn)
{
this.statusBarPanel1.Text="正在刷新文件夹,请稍等.....";
this.Cursor=Cursors.WaitCursor;
this.LisrimageList2.Images.Clear();
this.LisrimageList.Images.Clear();
listView1.SmallImageList=LisrimageList;
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
LisrimageList.Images.Add(ic0);
LisrimageList2.Images.Add(ic0);
listView1.Clear();
//设置列表框的表头
listView1.Columns.Add("文件名",160,HorizontalAlignment.Left);
listView1.Columns.Add("文件大小",120,HorizontalAlignment.Left);
listView1.Columns.Add("创建时间",120,HorizontalAlignment.Left);
listView1.Columns.Add("访问时间",200,HorizontalAlignment.Left);
string strPath=tn.FullPath;
strPath=strPath.Remove(0,5);
//获得当前目录下的所有文件
DirectoryInfo curDir=new DirectoryInfo(strPath);//创建目录对象。
FileInfo[] dirFiles;
try
{
dirFiles=curDir.GetFiles();
}
catch { return;}
string []arrSubItem=new string[4];
//文件的创建时间和访问时间。
int iCount=0; int iconIndex=1;//用1,而不用0是要让过0号图标。
foreach(FileInfo fileInfo in dirFiles)
{
string strFileName=fileInfo.Name;
//如果不是文件pagefile.sys
if(! strFileName.Equals("pagefile.sys"))
{
arrSubItem[0]=strFileName;
arrSubItem[1]=fileInfo.Length+" 字节";
arrSubItem[2]=fileInfo.CreationTime.ToString();
arrSubItem[3]=fileInfo.LastAccessTime.ToString();
}
else
{ arrSubItem[1]="未知大小"; arrSubItem[2]="未知日期"; arrSubItem[3]="未知日期";}
//得到每个文件的图标
string str=fileInfo.FullName;
try
{
SetIcon(LisrimageList,str,false);
SetIcon(LisrimageList2,str,true);
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
//插入列表项
ListViewItem LiItem=new ListViewItem(arrSubItem,iconIndex);
listView1.Items.Insert(iCount,LiItem);
iCount++;
iconIndex++;
}
strFilePath=strPath;
textBox1.Text=strPath;
this.statusBarPanel1.Text=strPath;
this.statusBarPanel2.Text="文件数量: " + iCount.ToString()+"个";
this.Cursor=Cursors.Arrow;
//以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。
int iItem=0;
DirectoryInfo Dir=new DirectoryInfo(strPath);
foreach(DirectoryInfo di in Dir.GetDirectories())
{
ListViewItem LiItem=new ListViewItem(di.Name,0);
listView1.Items.Insert(iItem,LiItem);
iItem++;
}
}
//*************************************************************************************
protected virtual void InitList2(string strName)
{
this.statusBarPanel1.Text="正在刷新文件夹,请稍等.....";
this.Cursor=Cursors.WaitCursor;
this.LisrimageList2.Images.Clear();
this.LisrimageList.Images.Clear();
listView1.SmallImageList=LisrimageList;
Icon ic0=myExtractIcon("%SystemRoot%\\system32\\shell32.dll",3);
LisrimageList.Images.Add(ic0);
LisrimageList2.Images.Add(ic0);
listView1.Clear();
//设置列表框的表头
listView1.Columns.Add("文件名",160,HorizontalAlignment.Left);
listView1.Columns.Add("文件大小",120,HorizontalAlignment.Left);
listView1.Columns.Add("创建时间",120,HorizontalAlignment.Left);
listView1.Columns.Add("访问时间",200,HorizontalAlignment.Left);
//获得当前目录下的所有文件
DirectoryInfo curDir=new DirectoryInfo(strName);//创建目录对象。
FileInfo[] dirFiles;
try
{
dirFiles=curDir.GetFiles();
}
catch { return;}
string []arrSubItem=new string[4];
//文件的创建时间和访问时间。
int iCount=0; int iconIndex=1;//用1,而不用0是要让过0号图标。
foreach(FileInfo fileInfo in dirFiles)
{
string strFileName=fileInfo.Name;
//如果不是文件pagefile.sys
if(! strFileName.Equals("pagefile.sys"))
{
arrSubItem[0]=strFileName;
arrSubItem[1]=fileInfo.Length+" 字节";
arrSubItem[2]=fileInfo.CreationTime.ToString();
arrSubItem[3]=fileInfo.LastAccessTime.ToString();
}
else
{ arrSubItem[1]="未知大小"; arrSubItem[2]="未知日期"; arrSubItem[3]="未知日期";}
//得到每个文件的图标
string str=fileInfo.FullName;
try
{
SetIcon(LisrimageList,str,false);
SetIcon(LisrimageList2,str,true);
}
catch(Exception ex)
{ MessageBox.Show(ex.Message,"错误提示",0,MessageBoxIcon.Error);}
//插入列表项
ListViewItem LiItem=new ListViewItem(arrSubItem,iconIndex);
listView1.Items.Insert(iCount,LiItem);
iCount++;
iconIndex++;//必须加在listView1.Items.Insert(iCount,LiItem);
}
strFilePath=strName;//把路径赋值于全局变量strFilePath
textBox1.Text=strName;
this.statusBarPanel2.Text="文件数量: " + iCount.ToString()+"个";
this.Cursor=Cursors.Arrow;
//以下是向列表框中插入目录,不是文件。获得当前目录下的各个子目录。
int iItem=0;//调用listView1.Items.Insert(iItem,LiItem);时用。不能使用iconIndex。
DirectoryInfo Dir=new DirectoryInfo(strName);//创建目录对象。
foreach(DirectoryInfo di in Dir.GetDirectories())
{
ListViewItem LiItem=new ListViewItem(di.Name,0);
listView1.Items.Insert(iItem,LiItem);
iItem++;
}
}
//*************************************************************************************
private void treeView1_AfterSelect_1(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
if(e.Node.Text=="我的电脑")
{ return;}
InitList(e.Node);
}
//*************************************************************************************
private void button1_Click(object sender, System.EventArgs e)
{
this.panel1.Hide();
}
//*************************************************************************************
private void listView1_ItemActivate(object sender, System.EventArgs e)
{
string str=Path.Combine(strFilePath,listView1.FocusedItem.Text);
try
{
if(listView1.FocusedItem.SubItems.Count>1)
{ System.Diagnostics.Process.Start(str); }
else
{ InitList2(str); }
}
catch { return;}
}
//*************************************************************************************
private void menuItem3_Click(object sender, System.EventArgs e)
{
this.panel1.Visible=! panel1.Visible;
}
//*************************************************************************************
private void menuItem5_Click(object sender, System.EventArgs e)
{
ListViewAB(1);
}
//*************************************************************************************
private void menuItem6_Click(object sender, System.EventArgs e)
{
ListViewAB(2);
}
//*************************************************************************************
private void menuItem7_Click(object sender, System.EventArgs e)
{
ListViewAB(3);
}
//*************************************************************************************
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -