C# 创建开机启动核心代码
#region 访问 AppSettings
private void SaveAppSetting(string key, string value)
{
var config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);
var section = config.GetSection("appSettings") as AppSettingsSection;
if (section.Settings[key] == null)
section.Settings.Add(key, value ?? string.Empty);
else
section.Settings[key].Value = value ?? string.Empty;
config.Save(ConfigurationSaveMode.Modified, true);
}
private string GetAppSetting(string key)
{
var config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);
var section = config.GetSection("appSettings") as AppSettingsSection;
if (section.Settings[key] == null)
return string.Empty;
return section.Settings[key].Value;
}
#endregion
#region 开机自启
private bool AutoStart
{
get
{
var autoStart = GetAppSetting("autostart");
if (string.IsNullOrWhiteSpace(autoStart))
return false;
return true;
}
set
{
if (value)
{
SaveAppSetting(");
CreateShortcut();
}
else
{
SaveAppSetting("autostart", string.Empty);
DeleteShortcut();
}
}
}
private void CreateShortcut()
{
IWshShell_Class shell = new IWshShell_ClassClass();
var shortcut = shell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + Application.ProductName + ".lnk") as IWshShortcut;
shortcut.TargetPath = Application.ExecutablePath;
shortcut.Save();
}
private void DeleteShortcut()
{
var linkPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + Application.ProductName + ".lnk";
System.IO.File.Delete(linkPath);
}
#endregion
需要引用COM组件:Window Script Host Object Model
C# 创建开机启动核心代码的更多相关文章
- Delphi创建开机启动项的方法示例
Delphi可以通过创建开机启动项键值的方法,将程序添加到开机启动项中.通过本实例代码就可以为您的程序添加到快速启动中,随着Windows一起启动,开机即运行的程序.该实例代码简单,主要是通过添加注册 ...
- C# 开机启动代码
开机启动/关闭 代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...
- C# 程序设置开机启动(一)
一.示例使用VBScript创建启动文件,并添加到Windows系统的启动目录 默认情况下 所有用户启动目录:C:\ProgramData\Microsoft\Windows\Start Menu\P ...
- 完美实现开机启动虚拟WIFI,顺便实现目前的WP8系统使用VPN(7.1修)
众所周知,windows7系统的机器若带有无线网卡(台式机可以买一个USB无线网卡,京东目前39元,TP-Link的),可以虚拟出wifi,供手机等移动设备使用. 虚拟的WIFI的命了和软件在网上都找 ...
- JavaFx 创建快捷方式及设置开机启动
原文地址:JavaFx 创建快捷方式及设置开机启动 | Stars-One的杂货小窝 原本是想整个桌面启动器,需要在windows平台上实现开机启动,但我的软件都是jar文件,不是传统的exe文件,也 ...
- android启动第一个界面时即闪屏的核心代码(两种方式)
闪屏,就是SplashScreen,也能够说是启动画面,就是启动的时候,闪(展示)一下,持续数秒后.自己主动关闭. 第一种方式: android的实现很easy,使用Handler对象的postDe ...
- 创建httpd启动脚本并加入启动列表开机启动
第一步: cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd 第二步: vim /etc/init.d/httpd 在第一行#!/bin/s ...
- Centos创建定时任务和开机启动运行脚本
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArYAAADlCAIAAAAp5CPLAAAgAElEQVR4nNS8d3cj15nuW/wq91vc8d ...
- Linux创建Jenkins启动脚本以及开机启动服务
1.jenkins.sh #!/bin/bash ###主要目的用于开机启动服务,不然 启动jenkins.war包没有java -jar的权限 JAVA_HOME=/usr/lib/jdk1.8.0 ...
随机推荐
- vs2008下面wince错误MSDiscoCodeGenerator
引用webservice时候错误.网上也有很多解决方法,各不相同. 不多说了. 打开vs2008安装包 找到wcu目录下面有个NetCF 目录: 下面的NetCFSetupv2.msi,NetCFSe ...
- iOS仿直播带有气泡动画的UIButton
现在直播软件确实很火,因为需要就写了一个带有动画气泡的按钮,代码中的部分动画有参考到其他童鞋,在这里万分感谢! .h文件 @interface YYBubbleButton : UIButton @p ...
- this指向
以前不太理解面向对象的this指向问题,今天自己看着视频教程,加自己学了2个例子,终于明白点了. 我们在写对象程序的时候,我们希望保持this始终是指向对象的,但事实确常常事与愿违. 正常情况this ...
- c++ float能到小数点后多少位
float xiaoshu=0.0000000000000000000000000000000000000000000001; cout<<"xiaoshu"<& ...
- DataTable,DataGridVIew转换到xls 方法 (转)
private void dataTableToCsv(DataTable table, string file) { string title = ""; FileStream ...
- caffe 基本知识简介
很多不错的网页: 1.http://alanse7en.github.io/caffedai-ma-jie-xi-1/ 主要介绍基本caffe知识 interace 接口 API中的‘I’ Caffe ...
- Win10/UWP开发—使用Cortana语音与App后台Service交互
上篇文章中我们介绍了使用Cortana调用前台App,不熟悉的移步到:Win10/UWP开发—使用Cortana语音指令与App的前台交互,这篇我们讲讲如何使用Cortana调用App的后台任务,相比 ...
- xml 读取递归算法
xml 读取递归算法:
- etl工具
ETL 工具下载全集 包括 Informatica Datastage Cognos( 持续更新) Datastage 8.0 BT种子下载:http://files.cnblogs.com/ta ...
- [转]Jenkins使用 管理节点
现在我们已经搭建好了基本的Jenkins环境,在这一集里,我们说一说如何管理节点. 进入“系统管理”中的“管理节点”. 创建Windos系统的奴隶节点 先创建一台安装了Win7系统的虚拟机,作为Jen ...