Skip to content

Commit

Permalink
Merge pull request #246 from LovelyWei/dev
Browse files Browse the repository at this point in the history
添加开机自启选项
  • Loading branch information
AmazingDM authored Feb 18, 2020
2 parents 9f46d7a + 11afbc9 commit bf7a766
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Netch/Forms/SettingForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions Netch/Forms/SettingForm.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections;
using System.Net;
using System.Windows.Forms;
using TaskScheduler;

namespace Netch.Forms
{
Expand Down Expand Up @@ -58,6 +60,7 @@ private void SettingForm_Load(object sender, EventArgs e)
StartWhenOpenedCheckBox.Checked = Global.Settings.StartWhenOpened;
CheckUpdateWhenOpenedCheckBox.Checked = Global.Settings.CheckUpdateWhenOpened;
MinimizeWhenStartedCheckBox.Checked = Global.Settings.MinimizeWhenStarted;
RunAtStartup.Checked = Global.Settings.RunAtStartup;

Socks5PortTextBox.Text = Global.Settings.Socks5LocalPort.ToString();
HTTPPortTextBox.Text = Global.Settings.HTTPLocalPort.ToString();
Expand All @@ -74,6 +77,7 @@ private void SettingForm_Load(object sender, EventArgs e)
StopWhenExitedCheckBox.Text = Utils.i18N.Translate(StopWhenExitedCheckBox.Text);
StartWhenOpenedCheckBox.Text = Utils.i18N.Translate(StartWhenOpenedCheckBox.Text);
MinimizeWhenStartedCheckBox.Text = Utils.i18N.Translate(MinimizeWhenStartedCheckBox.Text);
RunAtStartup.Text = Utils.i18N.Translate(RunAtStartup.Text);
CheckUpdateWhenOpenedCheckBox.Text = Utils.i18N.Translate(CheckUpdateWhenOpenedCheckBox.Text);
ProfileCount_Label.Text = Utils.i18N.Translate(ProfileCount_Label.Text);

Expand Down Expand Up @@ -136,6 +140,46 @@ private void ControlButton_Click(object sender, EventArgs e)
Global.Settings.StartWhenOpened = StartWhenOpenedCheckBox.Checked;
Global.Settings.CheckUpdateWhenOpened = CheckUpdateWhenOpenedCheckBox.Checked;
Global.Settings.MinimizeWhenStarted = MinimizeWhenStartedCheckBox.Checked;
Global.Settings.RunAtStartup = RunAtStartup.Checked;

// 开机自启判断
TaskSchedulerClass scheduler = new TaskSchedulerClass();
scheduler.Connect(null, null, null, null);
ITaskFolder folder = scheduler.GetFolder("\\");
bool taskIsExists = false;
try
{
folder.GetTask("Netch Startup");
taskIsExists = true;
}
catch (Exception) { }

if (RunAtStartup.Checked)
{
if (taskIsExists)
folder.DeleteTask("Netch Startup", 0);

ITaskDefinition task = scheduler.NewTask(0);
task.RegistrationInfo.Author = "Netch";
task.RegistrationInfo.Description = "Netch run at startup.";
task.Principal.RunLevel = _TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;

task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
action.Path = System.Windows.Forms.Application.ExecutablePath;


task.Settings.ExecutionTimeLimit = "PT0S";
task.Settings.DisallowStartIfOnBatteries = false;
task.Settings.RunOnlyIfIdle = false;

folder.RegisterTaskDefinition("Netch Startup", task, (int)_TASK_CREATION.TASK_CREATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
}
else
{
if (taskIsExists)
folder.DeleteTask("Netch Startup", 0);
}

try
{
Expand Down
5 changes: 5 additions & 0 deletions Netch/Models/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public class Setting
/// </summary>
public bool MinimizeWhenStarted = false;

/// <summary>
/// 是否开机启动软件
/// </summary>
public bool RunAtStartup = false;

/// <summary>
/// 是否打开软件时检查更新
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions Netch/Netch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
<None Remove=".gitignore" />
</ItemGroup>

<ItemGroup>
<COMReference Include="TaskScheduler.dll">
<Guid>e34cb9f1-c7f7-424c-be29-027dcc09363a</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<WrapperTool>tlbimp</WrapperTool>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>false</EmbedInteropTypes>
</COMReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="DNS" Version="5.0.0" />
<PackageReference Include="DnsClient" Version="1.2.0" />
Expand Down
1 change: 1 addition & 0 deletions Netch/Resources/zh-CN
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"Settings": "设置",
"Start when opened": "打开软件时启动加速",
"Minimize when started": "启动加速后隐藏",
"Run at startup": "开机自动启动",
"Local Port": "本地端口",
"Allow other Devices to connect": "允许其他设备连入",
"Netmask": "子网掩码",
Expand Down

0 comments on commit bf7a766

Please sign in to comment.