windows编程
这篇文章是写作业时整理的,只有基础,仅当加深记忆。有错请提出!
windows注册表
windows服务
windows动态库
环境
windows 10
VS 2013
windows注册表
windows注册表的概念,这里就不详细说明,网络上很多。
修改注册表,首先要权限
1. 在注册表中,把当前用户的权限提升到可以修改,添加,删除。或者直接完全控制就行
2. 直接使用管理员打开VS就行(ps : 你的管理员有权限)。
使用的是Microsoft.Win32中的RegistryKey
我把使用注册表的时候需要的属性封转了一下
/* 注册表操作 */
class RegistryOperation
{
private RegistryKey rootKey; // 注册表根键
private string keyName; // 子项名称
private string name; // 项中值名称
private string value; // 值的数据
private RegistryValueKind type; // 值数据的类型
}
创建和打开注册表项
/* 创建注册表项 并返回*/
public RegistryKey GetRegsitryKey()
{
RegistryKey key;
try{
key = rootKey.OpenSubKey(this.keyName,true);
}
catch (System.ArgumentNullException)
{
key = rootKey.CreateSubKey(this.keyName);
}
return key;
}
删除注册表项的子项,这个没有封转(ps : 没有需求)
函数:
RegistryKey.deleteSubKey(String);设置,获取,删除 项中的值
public void setRegisteyValue(){
RegistryKey key = this.getRegsitryKey();
key.SetValue(this.name,this.value,this.type);
key.Flush();
key.Close();
}
public string getRegistryValue()
{
string info = "";
RegistryKey key = this.getRegsitryKey();
info = key.GetValue(this.name).ToString();
key.Flush();
key.Close();
return info;
}
public void deleteRegistryValue()
{
RegistryKey key = this.getRegsitryKey();
key.DeleteValue(this.name);
key.Flush();
key.Close();
}
判断子项是否存在
public bool isRegeditKeyExist()
{
RegistryKey key = this.getRegsitryKey();
string[] valuesNames = key.GetSubNames();
foreach (string _value in valuesNames)
{
if (_value == this.name)
{
key.Close();
return true;
}
}
key.Close();
return false;
}
判断项中是否有某个值
public bool isRegeditValueExist()
{
RegistryKey key = this.getRegsitryKey();
string[] values = key.GetValueNames();
foreach (string _value in values)
{
if (_value == this.name)
{
key.Close();
return true;
}
}
key.Close();
return false;
}
Demo : 设置注册表编辑器是否可用
/* true : 可用,false: 禁止 */
public void registry_setRegeditEdit(bool ifEdit)
string keyName = @"Software\Microsoft\Windows\CurrentVersion\Policies\System";
string name = @"DisableRegistryTools";
string value = ifEdit == true ? 1 : 0;
RegistryKey rootKey = Registry.CurrentUser;
RegistryOperation registry = new RegistryOperation(rootKey,keyName,name,value,RegistryValueKind.DWord);
registry.setRegisteyValue();
rootKey.Flush();
rootKey.Close();
return result;
}
windows服务
创建服务工程后,Service 继承 ServiceBase
有2个继承函数
- protected override void OnStart(string[] args)
服务启动的时候,运行 - protected override void OnStop()
服务关闭的时候运行
安装程序属性设置
- WindowsService1
Name : 名称
Description: 在服务安装后显示的简介
DisplayName:在服务安装后显示的名称
ServiceName : 之前创建的服务(Service : ServiceBase)
StartType: 启动类型 (一般:Automatic) - serviceProcessinstaller1
Name : 名称
Account : 账户 (一般 : localSystem),这个不设置正确,服务可以启动不了
安装服务
服务的安装程序是.NET 提供的
位置:C:\Windows\Microsoft.NET\Framework\v4.0.30319
这个是我的电脑的.NET的位置(系统不一样找找就好)
里面有一个installUtil.exe 的程序就是我们想要的。
安装之前先要把服务程序生成解决方案(生成exe程序)
安装的时候需要管理员启动DOS
命令如下 : C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil f:\dataspace\vs2013\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe
(这是我的程序的位置)
看起来特别麻烦
把C:\Windows\Microsoft.NET\Framework\v4.0.30319添加到环境变量的Path里面就好了
installutil ..\..\WindowsService1.exe
卸载服务
命令: installutil /u .\..\WindowsService1.exe
注意是/u 不是 \u 。
windows动态库
创建类库工程,程序就和平常的一样,生成解决方案就可以得到dll文件
调用dll
在新建的工程中 添加 引用 找到之前的dll文件
双击,就可以看到程序
using dll_name; 就可以使用了。