Skip to content

Commit da1ad6a

Browse files
committed
整理代码,添加内部移除管理码功能
1 parent cb82617 commit da1ad6a

22 files changed

+241
-216
lines changed

BootHelper/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using Microsoft.Win32;
2-
using System.IO;
2+
33
namespace BootHelper
44
{
55
internal class Program
66
{
7-
static void Main(string[] args)
7+
private static void Main(string[] args)
88
{
9-
RegistryKey key=Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
10-
string path=Directory.GetCurrentDirectory()+"\\TimeControlConsole.exe";
11-
key.SetValue("TimeControl",path);
9+
RegistryKey key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
10+
string path = Directory.GetCurrentDirectory() + "\\TimeControlConsole.exe";
11+
key.SetValue("TimeControl", path);
1212
if (args.Length > 0)
1313
{
1414
if (args[0] == "remove")
File renamed without changes.

TimeControl/AppControl/App.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using System.IO;
7-
using System.Diagnostics;
1+
using System.Diagnostics;
82
using System.Windows.Forms;
93
using TimeControl.Tools;
104

@@ -14,6 +8,7 @@ public class App
148
{
159
public AppInformation appInformation = new();
1610
internal int tempTime = 0;
11+
1712
/// <summary>
1813
/// 返回进程的简要概述
1914
/// </summary>
@@ -22,12 +17,14 @@ public override string ToString()
2217
{
2318
return appInformation.name + " 已使用:" + TimeConvert.DescribeTime(appInformation.time);
2419
}
20+
2521
public App(string name, int time, int restInterval)
2622
{
2723
appInformation.time = time;
2824
appInformation.name = name;
2925
appInformation.restInterval = restInterval;
3026
}
27+
3128
public App(AppInformation appInformation)
3229
{
3330
this.appInformation = appInformation;
@@ -41,6 +38,7 @@ public virtual void Run()
4138
appInformation.time++;
4239
CheckRest();
4340
}
41+
4442
internal void CheckRest()
4543
{
4644
tempTime++;
@@ -51,13 +49,15 @@ internal void CheckRest()
5149
MessageBoxIcon.Warning);
5250
}
5351
}
52+
5453
/// <summary>
5554
/// 重设时间
5655
/// </summary>
5756
public void Reset()
5857
{
5958
appInformation.time = 0;
6059
}
60+
6161
public bool IsRunning()
6262
{
6363
Process[] processes = Process.GetProcessesByName(appInformation.name);
@@ -66,4 +66,4 @@ public bool IsRunning()
6666
return false;
6767
}
6868
}
69-
}
69+
}

TimeControl/AppControl/AppController.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Diagnostics;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
using System.Windows.Forms;
83
using System.IO;
4+
using System.Windows.Forms;
95
using TimeControl.Tools;
106

117
namespace TimeControl.AppControl
@@ -46,6 +42,7 @@ public void Refresh()
4642
}
4743
processMonitorTimer.Start();
4844
}
45+
4946
/// <summary>
5047
/// 根据名称添加进程
5148
/// </summary>
@@ -57,6 +54,7 @@ public void AddByName(string name, int restInterval)
5754
Save();
5855
Refresh();
5956
}
57+
6058
/// <summary>
6159
/// 根据名称添加时间受限的进程
6260
/// </summary>
@@ -69,6 +67,7 @@ public void AddByName(string name, int timeLimit, int restInterval)
6967
Save();
7068
Refresh();
7169
}
70+
7271
/// <summary>
7372
/// 跟踪所有进程,增加一秒
7473
/// </summary>
@@ -88,6 +87,7 @@ public void Run()
8887
}
8988
}
9089
}
90+
9191
/// <summary>
9292
/// 移除所列表所选的进程
9393
/// </summary>
@@ -99,6 +99,7 @@ public void Remove()
9999
Save();
100100
Refresh();
101101
}
102+
102103
/// <summary>
103104
/// 删除所有监控
104105
/// </summary>
@@ -109,6 +110,7 @@ public void RemoveAll()
109110
TimeControlFile.SaveToXML(apps);
110111
Refresh();
111112
}
113+
112114
/// <summary>
113115
/// 重设全部累计时间
114116
/// </summary>
@@ -123,9 +125,10 @@ public void Reset()
123125
Save();
124126
processMonitorTimer.Start();
125127
}
128+
126129
public void Save()
127130
{
128131
TimeControlFile.SaveToXML(apps);
129132
}
130133
}
131-
}
134+
}
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
8-
namespace TimeControl.AppControl
1+
namespace TimeControl.AppControl
92
{
103
public class AppInformation
114
{
@@ -14,4 +7,4 @@ public class AppInformation
147
public int restInterval = 0;
158
public int timeLimit = 0;
169
}
17-
}
10+
}

TimeControl/AppControl/LimitedApp.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using System.Diagnostics;
7-
using System.Windows.Forms;
8-
using System.IO;
1+
using System.Diagnostics;
92
using TimeControl.Tools;
103
using TimeControl.Windows;
114

125
namespace TimeControl.AppControl
136
{
147
public class LimitedApp : App
158
{
16-
179
public LimitedApp(string name, int time, int timeLimit, int restInterval) :
1810
base(name, time, restInterval)
1911
{
2012
appInformation.timeLimit = timeLimit;
2113
}
14+
2215
public LimitedApp(AppInformation appInformation) : base(appInformation)
2316
{ }
17+
2418
/// <summary>
2519
/// 运行一次(一秒),并根据情况显示警告或关闭进程,然后保存到文件
2620
/// </summary>
@@ -38,6 +32,7 @@ public override void Run()
3832
}
3933
CheckRest();
4034
}
35+
4136
/// <summary>
4237
/// 返回时间受限进程的简要概述
4338
/// </summary>
@@ -46,6 +41,7 @@ public override string ToString()
4641
{
4742
return base.ToString() + " 进程时间限制为:" + TimeConvert.DescribeTime(appInformation.timeLimit);
4843
}
44+
4945
/// <summary>
5046
/// 禁用掉该程序
5147
/// </summary>
@@ -59,4 +55,4 @@ public void Ban()
5955
appInformation.time = appInformation.timeLimit;
6056
}
6157
}
62-
}
58+
}

TimeControl/Program.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using System.Windows.Forms;
62
using System.Diagnostics;
73
using System.IO;
4+
using System.Windows.Forms;
85
using TimeControl.Tools;
96
using TimeControl.Windows;
107

118
namespace TimeControl
129
{
13-
static class Program
10+
internal static class Program
1411
{
1512
/// <summary>
1613
/// The main entry point for the application.
1714
/// </summary>
1815
[STAThread]
19-
static void Main(string[] args)
16+
private static void Main(string[] args)
2017
{
2118
IntPtr nowDesktop = Dllimport.GetThreadDesktop(Dllimport.GetCurrentThreadId());
2219
try
@@ -52,7 +49,7 @@ static void Main(string[] args)
5249
Application.SetCompatibleTextRenderingDefault(false);
5350
Application.Run(new ControlPanel(hide));
5451
}
55-
catch(Exception ex)
52+
catch (Exception ex)
5653
{
5754
Dllimport.SwitchDesktop(nowDesktop);
5855
File.Delete(TimeControlFile.TempTimeFile);
@@ -61,12 +58,12 @@ static void Main(string[] args)
6158
File.AppendAllText(TimeControlFile.LogFile, ex.ToString() + Environment.NewLine);
6259
if (ex is InvalidOperationException)
6360
{
64-
MessageBox.Show("可能发生了文件错误,请检查保存的文件是否有效或删除配置文件。","错误",
65-
MessageBoxButtons.OK,MessageBoxIcon.Error);
61+
MessageBox.Show("可能发生了文件错误,请检查保存的文件是否有效或删除配置文件。", "错误",
62+
MessageBoxButtons.OK, MessageBoxIcon.Error);
6663
}
67-
MessageBox.Show(ex.Message+Environment.NewLine+"以上为错误消息,已保存到"+TimeControlFile.LogFile
64+
MessageBox.Show(ex.Message + Environment.NewLine + "以上为错误消息,已保存到" + TimeControlFile.LogFile
6865
+ ",请反馈此问题到项目主页Issue。保护程序已暂时关闭。", "TimeControl发生错误",
69-
MessageBoxButtons.OK,MessageBoxIcon.Error);
66+
MessageBoxButtons.OK, MessageBoxIcon.Error);
7067
Process.Start("explorer.exe", "https://gitee.com/Sam-Hou/ComputerTimeControl/issues");
7168
//关闭保护进程
7269
Process[] processes = Process.GetProcessesByName("TimeControlConsole");
@@ -75,4 +72,4 @@ static void Main(string[] args)
7572
}
7673
}
7774
}
78-
}
75+
}

TimeControl/Tools/Command.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.IO;
5-
using System.Linq;
6-
using System.Text;
7-
using System.Threading.Tasks;
1+
using System.Diagnostics;
82

93
namespace TimeControl.Tools
104
{
@@ -20,6 +14,7 @@ public static void RunCommand(string command)
2014
process.StandardInput.WriteLine(command);
2115
process.StandardInput.WriteLine("exit");
2216
}
17+
2318
public static void RunCommand(string[] commands)
2419
{
2520
foreach (string command in commands)
@@ -28,4 +23,4 @@ public static void RunCommand(string[] commands)
2823
}
2924
}
3025
}
31-
}
26+
}

TimeControl/Tools/Dllimport.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62
using System.Runtime.InteropServices;
73

84
namespace TimeControl.Tools
95
{
10-
static class Dllimport
6+
internal static class Dllimport
117
{
12-
138
[Flags]
149
public enum ACCESS_MASK : uint
1510
{
@@ -97,12 +92,12 @@ public static extern IntPtr CreateDesktop(
9792
[DllImport("user32.dll")]
9893
public static extern bool SwitchDesktop(IntPtr hDesktop);
9994

100-
10195
[DllImport("user32.dll", SetLastError = true)]
10296
public static extern bool SetThreadDesktop(IntPtr hDesktop);
10397

10498
[DllImport("user32.dll", SetLastError = true)]
10599
public static extern IntPtr GetThreadDesktop(uint dwThreadId);
100+
106101
[DllImport("kernel32.dll")]
107102
internal static extern bool CreateProcess(
108103
string lpApplicationName,
@@ -116,6 +111,7 @@ internal static extern bool CreateProcess(
116111
ref STARTUPINFO lpStartupInfo,
117112
ref PROCESS_INFORMATION lpProcessInformation
118113
);
114+
119115
[StructLayout(LayoutKind.Sequential)]
120116
internal struct PROCESS_INFORMATION
121117
{
@@ -148,4 +144,4 @@ internal struct STARTUPINFO
148144
public IntPtr hStdError;
149145
}
150146
}
151-
}
147+
}

TimeControl/Tools/Password.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using System.Security.Cryptography;
42
using System.Text;
5-
using System.Threading.Tasks;
6-
using System.Security.Cryptography;
73

84
namespace TimeControl.Tools
95
{
@@ -17,4 +13,4 @@ public static string ComputeHash(string str)
1713
return Encoding.UTF8.GetString(md5.ComputeHash(bytes));
1814
}
1915
}
20-
}
16+
}

0 commit comments

Comments
 (0)