打包系统托盘文件
步骤一:添加控件 notifyIcon
步骤二:设置控件属性
- 设置主窗体属性为 showintask=false
- 添加控件单击代码以及主窗体程序的大小代码
在属性中找到对应事件单击
public Form1()
{
InitializeComponent(); //主窗体需要添加此行引用
//this.windowstate=formwindowstate.minimized //添加此行则在程序初始运行时最小化
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = true;
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible = true;
}
}
设置开机自启动
- 生成注册表
在 HKEY_CURRENT_USER 下作如下操作- 生成路径
- 在Run下添加 字符串值[string value] 进行命名
- 修改添加的字符串值的value data,将要运行的程序的启动路径写入
- 生成路径
- 写入程序代码 app.mainfest ,修改运行等级为 requireAdministrator
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC 清单选项
如果要更改 Windows 用户帐户控制级别,请用以下节点之一替换
requestedExecutionLevel 节点。
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
指定 requestedExecutionLevel 节点将会禁用文件和注册表虚拟化。
如果要利用文件和注册表虚拟化实现向后
兼容性,则删除 requestedExecutionLevel 节点。
-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
<applicationRequestMinimum>
<PermissionSet Unrestricted="true" ID="Custom" SameSite="site" />
<defaultAssemblyRequest permissionSetReference="Custom" />
</applicationRequestMinimum>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- 此应用程序设计使用的所有 Windows 版本的列表。
Windows 将会自动选择最兼容的环境。-->
<!-- 如果应用程序设计为使用 Windows Vista,请取消注释以下 supportedOS 节点-->
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"></supportedOS>-->
<!-- 如果应用程序设计使用 Windows 7,请取消注释以下 supportedOS 节点-->
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>-->
<!-- 如果应用程序设计为使用 Windows 8,请取消注释以下 supportedOS 节点-->
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"></supportedOS>-->
<!-- 如果应用程序设计为使用 Windows 8.1,请取消对以下 supportedOS 节点的注释-->
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>-->
</application>
</compatibility>
<!-- 启用 Windows 公共控件和对话框的主题(Windows XP 和更高版本) -->
<!-- <dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>-->
</asmv1:assembly>
衍生问题
1 . ClickOnce不支持请求执行级别"requireAdministrator"
- 选择主程序
- 选择菜单栏 PROJECT[项目]最后一项打开选项卡
- 选择 Security[安全性],把勾去掉
2 . 未处理System.BadImageFormatException,试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
原因:第一种为程序的运行以平台系统位数不匹配,第二种则是VS编译问题
- 将目标平台设置为 x86
参考内容1
参考内容2
参考内容3
参考内容4
参考内容5
参考内容6
更多问题后续补充
- warning 1:未能找到依赖的 dll
解决:将主输出/源文件/启动项的 Scan at build 改为 Properties Only - warning 2:无法创建 tag ID
解决:在General Information 中 将 Use Software Identification tag 改为 No