C#通用类库--设置开机自运行禁用任务管理器注册表等操作
生活随笔
收集整理的這篇文章主要介紹了
C#通用类库--设置开机自运行禁用任务管理器注册表等操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在做項目當中,有一項特殊的客戶需求就是軟件需要開機自運行,并且在運行期間不能進行關機等特定操作,需要屏蔽任務管理器和注冊表等,于是自己把這些寫成了通用的類,以后只要直接調用就行!
//類名:EcanSystem//作用:系統設置及其他
//作者:劉典武
//時間:2010-12-05
using?System;
using?System.Collections.Generic;
using?System.Text;
using?Microsoft.Win32;
using?System.Diagnostics;
using?System.Runtime.InteropServices;
using?System.Windows.Forms;
using?System.Drawing;
namespace?Ecan
{
????public?class?EcanSystem
????{
????????///?<summary>
????????///?設置程序開機運行
????????///?</summary>
????????///?<param?name="started">是否開機運行</param>
????????///?<param?name="exeName">要運行的EXE程序名稱(不要拓展名)</param>
????????///?<param?name="path">要運行的EXE程序路徑</param>
????????///?<returns>成功返回真,否則返回假</returns>
????????public?bool?runWhenStart(bool?started,?string?exeName,?string?path)
????????{
????????????RegistryKey?key?=?Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",?true);//打開注冊表子項
????????????if?(key?==?null)//如果該項不存在的話,則創建該子項
????????????{
????????????????key?=?Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
????????????}
????????????if?(started?==?true)
????????????{
????????????????try
????????????????{
????????????????????key.SetValue(exeName,?path);//設置為開機啟動
????????????????????key.Close();
????????????????}
????????????????catch
????????????????{
????????????????????return?false;
????????????????}
????????????}
????????????else
????????????{
????????????????try
????????????????{
????????????????????key.DeleteValue(exeName);//取消開機啟動
????????????????????key.Close();
????????????????}
????????????????catch
????????????????{
????????????????????return?false;
????????????????}
????????????}
????????????return?true;
????????}
????????///?<summary>
????????///?解禁任務管理器
????????///?</summary>
????????///?<returns>成功返回真,否則返回假</returns>
????????public?bool?enableTaskmgr()
????????{
????????????RegistryKey?key?=?Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system",?true);//打開注冊表子項
????????????if?(key?==?null)//如果該項不存在的話,則創建該子項
????????????{
????????????????key?=?Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
????????????}
????????????try
????????????{
????????????????key.SetValue("disabletaskmgr",?0,?RegistryValueKind.DWord);
????????????????key.Close();
????????????????return?true;
????????????}
????????????catch
????????????{
????????????????return?false;
????????????}
????????}
????????///?<summary>
????????///?禁用任務管理器
????????///?</summary>
????????///?<returns>成功返回真,否則返回假</returns>
????????public?bool?notEnableTaskmgr()
????????{
????????????RegistryKey?key?=?Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system",?true);//打開注冊表子項
????????????if?(key?==?null)//如果該項不存在的話,則創建該子項
????????????{
????????????????key?=?Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
????????????}
????????????try
????????????{
????????????????key.SetValue("disabletaskmgr",?1,?RegistryValueKind.DWord);
????????????????key.Close();
????????????????return?true;
????????????}
????????????catch
????????????{
????????????????return?false;
????????????}
????????}
????????///?<summary>
????????///?解禁注冊表
????????///?</summary>
????????///?<returns>成功返回真,否則返回假</returns>
????????public?bool?enableRegedit()
????????{
????????????RegistryKey?key?=?Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system",?true);//打開注冊表子項
????????????if?(key?==?null)//如果該項不存在的話,則創建該子項
????????????{
????????????????key?=?Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
????????????}
????????????try
????????????{
????????????????key.SetValue("disableregistrytools",?0,?RegistryValueKind.DWord);
????????????????key.Close();
????????????????return?true;
????????????}
????????????catch
????????????{
????????????????return?false;
????????????}
????????}
????????///?<summary>
????????///?禁用注冊表
????????///?</summary>
????????///?<returns>成功返回真,否則返回假</returns>
????????public?bool?notEnableRegedit()
????????{
????????????RegistryKey?key?=?Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system",?true);//打開注冊表子項
????????????if?(key?==?null)//如果該項不存在的話,則創建該子項
????????????{
????????????????key?=?Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
????????????}
????????????try
????????????{
????????????????key.SetValue("disableregistrytools",?1,?RegistryValueKind.DWord);
????????????????key.Close();
????????????????return?true;
????????????}
????????????catch
????????????{
????????????????return?false;
????????????}
????????}
????????///?<summary>
????????///?結束進程
????????///?</summary>
????????///?<param?name="processName">進程名稱</param>
????????///?<returns>成功返回真,否則返回假</returns>
????????public?bool?killProcess(string?processName)
????????{
????????????try
????????????{
????????????????foreach?(Process?p?in?Process.GetProcesses())
????????????????{
????????????????????if?(p.ProcessName?==?processName)
????????????????????{
????????????????????????p.Kill();
????????????????????}
????????????????}
????????????????return?true;
????????????}
????????????catch?{?return?false;?}
????????}
????????///?<summary>
????????///?注冊控件
????????///?</summary>
????????///?<param?name="dllIdValue">控件注冊后對應的鍵值</param>
????????///?<returns>成功返回真,否則返回假</returns>
????????public?bool?regDll(string?dllIdValue)
????????{
????????????try
????????????{
????????????????RegistryKey?key?=?Registry.ClassesRoot.OpenSubKey(@"CLSTD\"?+?dllIdValue,?true);//打開注冊表子項
????????????????if?(key?==?null)//如果該項不存在的話,則創建該子項
????????????????{
????????????????????key?=?Registry.ClassesRoot.CreateSubKey(@"CLSTD\"?+?dllIdValue);
????????????????}
????????????????return?true;
????????????}
????????????catch?{?return?false;?}
????????}
????????///?<summary>
????????///?壓縮圖片(指定壓縮比例值)
????????///?</summary>
????????///?<param?name="fromFile">源文件</param>
????????///?<param?name="saveFile">保存文件</param>
????????///?<param?name="bili">比例值(例如0.5)</param>
????????///?<returns>成功返回真,否則返回假</returns>
????????public?bool?pressImage(string?fromFile,?string?saveFile,double?bili)
????????{
????????????Image?img;
????????????Bitmap?bmp;
????????????Graphics?grap;
????????????int?width,?height;
????????????try
????????????{
????????????????img?=?Image.FromFile(fromFile);
????????????????width?=?Convert.ToInt32(img.Width?*?bili);
????????????????height?=?Convert.ToInt32(img.Height?*?bili);
????????????????bmp?=?new?Bitmap(width,?height);
????????????????grap?=?Graphics.FromImage(bmp);
????????????????grap.SmoothingMode?=?System.Drawing.Drawing2D.SmoothingMode.HighQuality;
????????????????grap.InterpolationMode?=?System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
????????????????grap.DrawImage(img,?new?Rectangle(0,?0,?width,?height));
????????????????bmp.Save(saveFile,?System.Drawing.Imaging.ImageFormat.Jpeg);
????????????????grap.Dispose();
????????????????bmp.Dispose();
????????????????img.Dispose();
????????????????return?true;
????????????}
????????????catch?{?return?false;?}
????????}
????????///?<summary>
????????///?壓縮圖片(指定高度和寬度)
????????///?</summary>
????????///?<param?name="fromFile">源文件</param>
????????///?<param?name="saveFile">保存文件</param>
????????///?<param?name="width">寬度值</param>
????????///?<param?name="height">高度值</param>
????????///?<returns>成功返回真,否則返回假</returns>
????????public?bool?pressImage(string?fromFile,?string?saveFile,?int?width,int?height)
????????{
????????????Image?img;
????????????Bitmap?bmp;
????????????Graphics?grap;
????????????try
????????????{
????????????????img?=?Image.FromFile(fromFile);
????????????????bmp?=?new?Bitmap(width,?height);
????????????????grap?=?Graphics.FromImage(bmp);
????????????????grap.SmoothingMode?=?System.Drawing.Drawing2D.SmoothingMode.HighQuality;
????????????????grap.InterpolationMode?=?System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
????????????????grap.DrawImage(img,?new?Rectangle(0,?0,?width,?height));
????????????????bmp.Save(saveFile,?System.Drawing.Imaging.ImageFormat.Jpeg);
????????????????
????????????????grap.Dispose();
????????????????bmp.Dispose();
????????????????img.Dispose();
????????????????return?true;
????????????}
????????????catch?{?return?false;?}
????????}
????}
}
?
一個C#資源分享平臺,專業分享學習高質量代碼,每周期布置學習任務,激發學習C#興趣!(QQ群:128874886)總結
以上是生活随笔為你收集整理的C#通用类库--设置开机自运行禁用任务管理器注册表等操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最近安装了win2008R2,界面比wi
- 下一篇: Linkify介绍 编辑textview