C#小程序桌面快捷方式启动LaunchPad 详细代码源码Word文档格式.docx
《C#小程序桌面快捷方式启动LaunchPad 详细代码源码Word文档格式.docx》由会员分享,可在线阅读,更多相关《C#小程序桌面快捷方式启动LaunchPad 详细代码源码Word文档格式.docx(14页珍藏版)》请在冰点文库上搜索。
![C#小程序桌面快捷方式启动LaunchPad 详细代码源码Word文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/2/6bc85f67-1215-47a2-9437-5d1cd08e3529/6bc85f67-1215-47a2-9437-5d1cd08e35291.gif)
然后添加自己准备好的图标,自己随便找个。
我喜欢机器猫,就放了一个。
改名Icon。
五、开始编写FZHelper.cs类
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceLaunchPad
{
publicstaticclassFZHelp
{
publicstaticstringstrWeiZhui="
lnk,url,pif,exe"
;
//快捷方式尾缀,可以按照自己的需要随时添加
///<
summary>
///把MessageBox.Show放到类里方便调用和修改。
/summary>
paramname="
neirong"
>
<
/param>
publicstaticvoidMsgBox(stringneirong)
System.Windows.Forms.MessageBox.Show(neirong.ToString());
}
///获取当前APP的路径
returns>
/returns>
publicstaticstringGetAppPath()
stringstr="
"
str=AppDomain.CurrentDomain.BaseDirectory.ToString();
//str=System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//当前APP的名称
returnstr;
#region检查尾缀是否包含类型里
///检查尾缀是否包含类型里
strPath"
文件路径<
TRUEORFALSE<
publicstaticboolCheckWeiZhui(stringstrPath)
boolflag=false;
stringstrExtension=FZHelp.getExtension(strPath);
//获取当前文件路径的尾缀
string[]ss=FZHelp.strWeiZhui.Split('
'
);
//按照,来分割字符串到数组ss里
if(ss.Contains(strExtension))//比较尾缀是否包含在内
flag=true;
returnflag;
#endregion
#region获得指定路径的扩展名getExtension(stringpara)
///获得指定路径的扩展名(或尾缀)
para"
指定路径<
扩展名<
publicstaticstringgetExtension(stringpara)
para.Replace("
\\"
"
//去掉特殊符号\
intnIndex=para.LastIndexOf("
."
//获取路径里所有的.的数量,以便于定位扩展名的位置
if(nIndex>
0)//判断是否有.
para=para.Substring(nIndex+1);
//加1是为了不带"
,比如不是.exe
returnpara;
}
六、右侧Form1.cs右键–》查看代码,开始编写
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Windows.Forms;
usingSystem.Xml;
usingSystem.IO;
usingSystem.Diagnostics;
publicpartialclassForm1:
Form
publicstringcurrentPath="
//当前所选的快捷方式完整路径
publicList<
string>
strLink=newList<
();
//所有的快捷方式保存到这个数组里
privateboolblformMax=false;
//当前窗体状态,true是显示,false是隐藏
publicForm1()
HideForm();
//隐藏当前窗体
InitializeComponent();
//初始化控件
notifyIcon1.Icon=Properties.Resources.Icon;
//设置托盘图标
notifyIcon1.Text=this.Text;
//设置托盘显示内容
this.Icon=Properties.Resources.Icon;
//设置窗体图标
setLink(FZHelp.GetAppPath());
//获取当前APP目录下所有的快捷方式并放入托盘中
///隐藏当前窗体
privatevoidHideForm()
this.ShowInTaskbar=false;
this.Hide();
//窗体隐藏
this.WindowState=FormWindowState.Minimized;
///获取当前APP目录下所有的快捷方式
path"
privatevoidsetLink(stringpath)
DirectoryInfoinfo=newDirectoryInfo(path);
DirectoryInfo[]dirList=info.GetDirectories();
for(inti=0;
i<
dirList.Length;
i++)
setLink(dirList[i].FullName);
//如果有子目录就循环
foreach(FileInfofiiininfo.GetFiles())
{
//MessageBox.Show("
尾缀="
+FZHelp.getExtension(fii.FullName));
if(FZHelp.CheckWeiZhui(fii.FullName))
+fii.Name+"
||"
+newFileInfo(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName).Name);
if(fii.Name!
=newFileInfo(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName).Name)//当前快捷方式的名称不能跟自己的名字相同
currentPath=fii.FullName;
//获取所选快捷方式的名字
strLink.Add(currentPath+"
文件"
//添加到数组里
listBox1.Items.Add(currentPath+"
+fii.Name);
//添加到当前窗体的ListBox1控件里
SetcontextMenuStrip(currentPath);
//设置快捷方式到托盘里,并且添加事件
}
#region设置contextMenuStrip1
///把快捷方式放到托盘中,并为每一个快捷方式添加事件
privatevoidSetcontextMenuStrip(stringpath)
ToolStripMenuItemtsmItem=newToolStripMenuItem();
stringsname=path.Substring(path.LastIndexOf("
)+1);
//获取文件名
tsmItem.Text=sname;
//赋值
tsmItem.Click+=newEventHandler(ToolStripMenuItemClick);
//添加事件
contextMenuStrip1.Items.Add(tsmItem);
privatevoidToolStripMenuItemClick(objectsender,EventArgse)
ToolStripMenuItempara=(ToolStripMenuItem)sender;
if(currentPath!
="
&
&
currentPath!
=null)//路径不能为空
+para.Text);
//提示名称
try//异常处理
Process.Start(FZHelp.GetAppPath()+"
//运行程序
catch(Exception)
throw;
///退出控件
sender"
e"
privatevoid退出ToolStripMenuItem_Click(objectsender,EventArgse)
notifyIcon1.Dispose();
//释放托盘,为了在关闭程序后不再右下角显示托盘
Application.Exit();
//程序退出
///显示/隐藏窗体
privatevoid显示隐藏ToolStripMenuItem_Click(objectsender,EventArgse)
if(this.blformMax==false)//如果窗体隐藏,就显示
this.ShowInTaskbar=true;
this.Show();
this.blformMax=true;
else//如果窗体显示,就隐藏
this.blformMax=false;
///双击通知栏图标显示/隐藏窗体
privatevoidnotifyIcon1_MouseDoubleClick(objectsender,MouseEventArgse)
privatevoidbutton1_Click(objectsender,EventArgse)
///刷新当前程序
privatevoid刷新ToolStripMenuItem_Click(objectsender,EventArgse)
Application.Restart();
以上为完整代码