mirror of
https://github.com/jie65535/KeyGo.git
synced 2025-06-02 17:39:13 +08:00
1. 发布 版本v1.0.0.0
2. 取消 管理员权限请求 3. 修改 自启动实现方法,现在使用快捷方式发布到启动目录的方式实现 4. 增加 快捷方式自启动实现方法帮助类
This commit is contained in:
parent
c7ac1341cc
commit
ddc54d9a97
178
KeyGo/AppAutoStart.cs
Normal file
178
KeyGo/AppAutoStart.cs
Normal file
@ -0,0 +1,178 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using IWshRuntimeLibrary;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace KeyGo
|
||||
{
|
||||
/// <summary>
|
||||
/// 摘自:https://blog.csdn.net/liyu3519/article/details/81257839
|
||||
/// 应用开机自启动帮助类
|
||||
/// </summary>
|
||||
static class AppAutoStart
|
||||
{
|
||||
/// <summary>
|
||||
/// 快捷方式名称-任意自定义
|
||||
/// </summary>
|
||||
private static string QuickName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 自动获取系统自动启动目录
|
||||
/// </summary>
|
||||
private static string SystemStartPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 自动获取程序完整路径
|
||||
/// </summary>
|
||||
private static string AppAllPath { get; }
|
||||
|
||||
static AppAutoStart()
|
||||
{
|
||||
var thisProcess = Process.GetCurrentProcess();
|
||||
QuickName = thisProcess.ProcessName;
|
||||
AppAllPath = thisProcess.MainModule.FileName;
|
||||
SystemStartPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
|
||||
Console.WriteLine(SystemStartPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置开机自动启动-只需要调用改方法就可以了参数里面的bool变量是控制开机启动的开关的,默认为开启自启启动
|
||||
/// </summary>
|
||||
/// <param name="onOff">自启开关</param>
|
||||
public static void SetMeAutoStart(bool onOff = true)
|
||||
{
|
||||
if (onOff)//开机启动
|
||||
{
|
||||
//获取启动路径应用程序快捷方式的路径集合
|
||||
List<string> shortcutPaths = GetQuickFromFolder(SystemStartPath, AppAllPath);
|
||||
//存在2个以快捷方式则保留一个快捷方式-避免重复多于
|
||||
if (shortcutPaths.Count >= 2)
|
||||
{
|
||||
for (int i = 1; i < shortcutPaths.Count; i++)
|
||||
{
|
||||
DeleteFile(shortcutPaths[i]);
|
||||
}
|
||||
}
|
||||
else if (shortcutPaths.Count < 1)//不存在则创建快捷方式
|
||||
{
|
||||
CreateShortcut(SystemStartPath, QuickName, AppAllPath);
|
||||
}
|
||||
}
|
||||
else//开机不启动
|
||||
{
|
||||
//获取启动路径应用程序快捷方式的路径集合
|
||||
List<string> shortcutPaths = GetQuickFromFolder(SystemStartPath, AppAllPath);
|
||||
//存在快捷方式则遍历全部删除
|
||||
if (shortcutPaths.Count > 0)
|
||||
{
|
||||
for (int i = 0; i < shortcutPaths.Count; i++)
|
||||
{
|
||||
DeleteFile(shortcutPaths[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向目标路径创建指定文件的快捷方式
|
||||
/// </summary>
|
||||
/// <param name="directory">目标目录</param>
|
||||
/// <param name="shortcutName">快捷方式名字</param>
|
||||
/// <param name="targetPath">文件完全路径</param>
|
||||
/// <param name="description">描述</param>
|
||||
/// <param name="iconLocation">图标地址</param>
|
||||
/// <returns>成功或失败</returns>
|
||||
private static bool CreateShortcut(string directory, string shortcutName, string targetPath, string description = null, string iconLocation = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); //目录不存在则创建
|
||||
//添加引用 Com 中搜索 Windows Script Host Object Model
|
||||
string shortcutPath = Path.Combine(directory, $"{shortcutName}.lnk"); //合成路径
|
||||
WshShell shell = new WshShell();
|
||||
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath); //创建快捷方式对象
|
||||
shortcut.TargetPath = targetPath; //指定目标路径
|
||||
shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); //设置起始位置
|
||||
shortcut.WindowStyle = 1; //设置运行方式,默认为常规窗口
|
||||
shortcut.Description = description; //设置备注
|
||||
shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation; //设置图标路径
|
||||
shortcut.Save(); //保存快捷方式
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string temp = ex.Message;
|
||||
temp = "";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定文件夹下指定应用程序的快捷方式路径集合
|
||||
/// </summary>
|
||||
/// <param name="directory">文件夹</param>
|
||||
/// <param name="targetPath">目标应用程序路径</param>
|
||||
/// <returns>目标应用程序的快捷方式</returns>
|
||||
private static List<string> GetQuickFromFolder(string directory, string targetPath)
|
||||
{
|
||||
List<string> tempStrs = new List<string>();
|
||||
tempStrs.Clear();
|
||||
string tempStr = null;
|
||||
string[] files = Directory.GetFiles(directory, "*.lnk");
|
||||
if (files == null || files.Length < 1)
|
||||
{
|
||||
return tempStrs;
|
||||
}
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
//files[i] = string.Format("{0}\\{1}", directory, files[i]);
|
||||
tempStr = GetAppPathFromQuick(files[i]);
|
||||
if (tempStr == targetPath)
|
||||
{
|
||||
tempStrs.Add(files[i]);
|
||||
}
|
||||
}
|
||||
return tempStrs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取快捷方式的目标文件路径-用于判断是否已经开启了自动启动
|
||||
/// </summary>
|
||||
/// <param name="shortcutPath"></param>
|
||||
/// <returns></returns>
|
||||
private static string GetAppPathFromQuick(string shortcutPath)
|
||||
{
|
||||
//快捷方式文件的路径 = @"d:\Test.lnk";
|
||||
if (System.IO.File.Exists(shortcutPath))
|
||||
{
|
||||
WshShell shell = new WshShell();
|
||||
IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortcutPath);
|
||||
//快捷方式文件指向的路径.Text = 当前快捷方式文件IWshShortcut类.TargetPath;
|
||||
//快捷方式文件指向的目标目录.Text = 当前快捷方式文件IWshShortcut类.WorkingDirectory;
|
||||
return shortct.TargetPath;
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据路径删除文件-用于取消自启时从计算机自启目录删除程序的快捷方式
|
||||
/// </summary>
|
||||
/// <param name="path">路径</param>
|
||||
private static void DeleteFile(string path)
|
||||
{
|
||||
FileAttributes attr = System.IO. File.GetAttributes(path);
|
||||
if (attr == FileAttributes.Directory)
|
||||
{
|
||||
Directory.Delete(path, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.IO.File.Delete(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -4,8 +4,6 @@ using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Windows.Forms;
|
||||
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace KeyGo
|
||||
{
|
||||
public partial class FormMain : Form
|
||||
@ -302,26 +300,11 @@ namespace KeyGo
|
||||
{
|
||||
try
|
||||
{
|
||||
if (enable)
|
||||
{
|
||||
RegistryKey R_local = Registry.CurrentUser;
|
||||
RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
|
||||
R_run.SetValue("KeyGo", Application.ExecutablePath);
|
||||
R_run.Close();
|
||||
R_local.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
RegistryKey R_local = Registry.CurrentUser;
|
||||
RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
|
||||
R_run.DeleteValue("KeyGo", false);
|
||||
R_run.Close();
|
||||
R_local.Close();
|
||||
}
|
||||
AppAutoStart.SetMeAutoStart(enable);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show("注册表编辑失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
MessageBox.Show("设置开机自启时异常:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,9 +61,7 @@
|
||||
<PropertyGroup>
|
||||
<GenerateManifests>false</GenerateManifests>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup />
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
@ -78,6 +76,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AppAutoStart.cs" />
|
||||
<Compile Include="AppConfig.cs" />
|
||||
<Compile Include="AppControl.cs" />
|
||||
<Compile Include="AppHotKey.cs" />
|
||||
@ -131,7 +130,6 @@
|
||||
<EmbeddedResource Include="UCHotKeyItem.resx">
|
||||
<DependentUpon>UCHotKeyItem.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
<None Include="Properties\app.manifest" />
|
||||
<None Include="Properties\Settings.settings">
|
||||
<Generator>SettingsSingleFileGenerator</Generator>
|
||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
||||
@ -145,7 +143,17 @@
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<ItemGroup>
|
||||
<COMReference Include="IWshRuntimeLibrary">
|
||||
<Guid>{F935DC20-1CF0-11D0-ADB9-00C04FD58A0B}</Guid>
|
||||
<VersionMajor>1</VersionMajor>
|
||||
<VersionMinor>0</VersionMinor>
|
||||
<Lcid>0</Lcid>
|
||||
<WrapperTool>tlbimp</WrapperTool>
|
||||
<Isolated>False</Isolated>
|
||||
<EmbedInteropTypes>True</EmbedInteropTypes>
|
||||
</COMReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="KeyGo.ico" />
|
||||
<Content Include="Resources\ImgAdd.png" />
|
||||
|
@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
|
||||
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
|
||||
//通过使用 "*",如下所示:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("0.1.2")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
@ -1,69 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<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 节点。n
|
||||
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
|
||||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
|
||||
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
|
||||
|
||||
指定 requestedExecutionLevel 元素将禁用文件和注册表虚拟化。
|
||||
如果你的应用程序需要此虚拟化来实现向后兼容性,则删除此
|
||||
元素。
|
||||
-->
|
||||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
|
||||
</requestedPrivileges>
|
||||
<applicationRequestMinimum>
|
||||
<defaultAssemblyRequest permissionSetReference="Custom" />
|
||||
<PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
|
||||
</applicationRequestMinimum>
|
||||
</security>
|
||||
</trustInfo>
|
||||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
|
||||
<application>
|
||||
<!-- 设计此应用程序与其一起工作且已针对此应用程序进行测试的
|
||||
Windows 版本的列表。取消评论适当的元素,
|
||||
Windows 将自动选择最兼容的环境。 -->
|
||||
<!-- Windows Vista -->
|
||||
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
|
||||
<!-- Windows 7 -->
|
||||
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
|
||||
<!-- Windows 8 -->
|
||||
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
|
||||
<!-- Windows 8.1 -->
|
||||
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
|
||||
<!-- Windows 10 -->
|
||||
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
|
||||
</application>
|
||||
</compatibility>
|
||||
<!-- 指示该应用程序可以感知 DPI 且 Windows 在 DPI 较高时将不会对其进行
|
||||
自动缩放。Windows Presentation Foundation (WPF)应用程序自动感知 DPI,无需
|
||||
选择加入。选择加入此设置的 Windows 窗体应用程序(目标设定为 .NET Framework 4.6 )还应
|
||||
在其 app.config 中将 "EnableWindowsFormsHighDpiAutoResizing" 设置设置为 "true"。-->
|
||||
<!--
|
||||
<application xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<windowsSettings>
|
||||
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
|
||||
</windowsSettings>
|
||||
</application>
|
||||
-->
|
||||
<!-- 启用 Windows 公共控件和对话框的主题(Windows XP 和更高版本) -->
|
||||
<!--
|
||||
<dependency>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity
|
||||
type="win32"
|
||||
name="Microsoft.Windows.Common-Controls"
|
||||
version="6.0.0.0"
|
||||
processorArchitecture="*"
|
||||
publicKeyToken="6595b64144ccf1df"
|
||||
language="*"
|
||||
/>
|
||||
</dependentAssembly>
|
||||
</dependency>
|
||||
-->
|
||||
</assembly>
|
Loading…
Reference in New Issue
Block a user