Skip to content

Commit b850022

Browse files
committed
自动保存时间并加载
1 parent 08c4f80 commit b850022

File tree

10 files changed

+145
-49
lines changed

10 files changed

+145
-49
lines changed

TimeControl/App.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using System.IO;
67

78
namespace TimeControl
89
{
910
public class App
1011
{
11-
private string name;
12-
private string location;
12+
private readonly string name;
1313
public string Name { get { return name; }}
1414
internal int time;
1515
/// <summary>
@@ -20,18 +20,20 @@ public override string ToString()
2020
{
2121
return Name + " 已使用 " + time+" 秒!";
2222
}
23-
public App(string name, string location)
23+
public App(string name,int time)
2424
{
25+
this.time = time;
2526
this.name = name;
26-
this.location = location;
27-
Reset();
2827
}
2928
/// <summary>
3029
/// 运行一次(一秒)
3130
/// </summary>
32-
public virtual void Run()
31+
public virtual void Run(StreamWriter streamWriter)
3332
{
3433
time++;
34+
streamWriter.WriteLine(Name);
35+
streamWriter.WriteLine(time);
36+
streamWriter.WriteLine("//");
3537
}
3638
/// <summary>
3739
/// 重设时间

TimeControl/ControlPanel.Designer.cs

Lines changed: 22 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TimeControl/ControlPanel.cs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public ControlPanel(bool hide)
2424
{
2525
InitializeComponent();
2626
this.hide = hide;
27-
if (File.Exists(PasswordFile.tcPassLocation))//加载密码哈希值
27+
if (File.Exists(TimeControlFile.tcPassLocation))//加载密码哈希值
2828
{
29-
unlockPasswordHash = Convert.ToInt32(File.ReadAllText(PasswordFile.tcPassLocation));
29+
unlockPasswordHash = Convert.ToInt32(File.ReadAllText(TimeControlFile.tcPassLocation));
3030
PasswordSet();
3131
}
3232
controller = new(usageBox, processMonitorTimer);
@@ -86,28 +86,24 @@ private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
8686
Process.Start("explorer.exe", "https://icons8.com/icon/19614/icon");
8787
}
8888

89-
private void AppAddButton_Click(object sender, EventArgs e)//添加打开的窗口
89+
private void AppAddButton_Click(object sender, EventArgs e)//添加进程
9090
{
9191
if (processNameBox.Text.ToLower() == "timecontrol" ||
9292
processNameBox.Text.ToLower() == "timecontrolconsole")
9393
{
9494
return;
9595
}
96-
TimeInput timeInput = new(controller, processNameBox.Text);
96+
TimeInput timeInput = new(controller, processNameBox.Text);//打开进程限时控制窗口
9797
timeInput.ShowDialog();
9898
}
9999

100-
private void RemoveButton_Click(object sender, EventArgs e)//移除所有的已添加窗口
100+
private void RemoveButton_Click(object sender, EventArgs e)//移除窗口
101101
{
102102
//检测密码设置
103-
if (unlockPasswordHash != 0)
103+
if (PasswordCheck())
104104
{
105-
PasswordInput passwordInput = new(unlockPasswordHash);
106-
if (passwordInput.ShowDialog() == DialogResult.OK)
107-
controller.Remove();
108-
}
109-
else
110105
controller.Remove();
106+
}
111107
}
112108

113109
private void RefreshButton_Click(object sender, EventArgs e)//重新获取所有软件所用时间
@@ -145,13 +141,34 @@ private void UnloackPasswordSetButton_Click(object sender, EventArgs e)//保存
145141
{
146142
unlockPasswordHash = unlockPasswordBox.Text.GetHashCode();//保存哈希值
147143
PasswordSet();
148-
File.WriteAllText(PasswordFile.tcPassLocation, unlockPasswordHash.ToString());//保存哈希值到文件
144+
File.WriteAllText(TimeControlFile.tcPassLocation, unlockPasswordHash.ToString());//保存哈希值到文件
149145
}
150146
private void PasswordSet()//密码设置后调用
151147
{
152148
unlockPasswordBox.Text = "";
153149
unlockPasswordBox.Enabled = false;
154150
unloackPasswordSetButton.Enabled = false;
155151
}
152+
153+
private void clearButton_Click(object sender, EventArgs e)//移除所有的已添加窗口
154+
{
155+
if (PasswordCheck())
156+
{
157+
controller.RemoveAll();
158+
}
159+
}
160+
private bool PasswordCheck()
161+
{
162+
if (unlockPasswordHash != 0)
163+
{
164+
PasswordInput passwordInput = new(unlockPasswordHash);
165+
if (passwordInput.ShowDialog() == DialogResult.OK)
166+
return true;
167+
else
168+
return false;
169+
}
170+
else
171+
return true;
172+
}
156173
}
157174
}

TimeControl/LimitedApp.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
using System.Threading.Tasks;
66
using System.Diagnostics;
77
using System.Windows.Forms;
8+
using System.IO;
89

910
namespace TimeControl
1011
{
1112
public class LimitedApp : App
1213
{
13-
private int timeLimit;
14-
public LimitedApp(string name, string location,int timeLimit) : base(name, location)
14+
private readonly int timeLimit;
15+
public LimitedApp(string name,int time,int timeLimit) : base(name,time)
1516
{
1617
this.timeLimit = timeLimit;
1718
}
1819
/// <summary>
1920
/// 运行一次(一秒),并根据情况显示警告或关闭进程
2021
/// </summary>
21-
public override void Run()
22+
public override void Run(StreamWriter streamWriter)
2223
{
23-
base.Run();
24+
time++;
2425
if (time == timeLimit - 30)
2526
{
2627
LimitWarningWindow warningWindow = new(this);
@@ -30,6 +31,10 @@ public override void Run()
3031
{
3132
Ban();
3233
}
34+
streamWriter.WriteLine(Name);
35+
streamWriter.WriteLine(time);
36+
streamWriter.WriteLine(timeLimit);
37+
streamWriter.WriteLine("//");
3338
}
3439
/// <summary>
3540
/// 返回时间受限进程的简要概述

TimeControl/ListController.cs

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,61 @@
55
using System.Text;
66
using System.Threading.Tasks;
77
using System.Windows.Forms;
8+
using System.IO;
89

910
namespace TimeControl
1011
{
11-
public class ListController
12+
public class ListController
1213
{
14+
private FileStream fileStream = new(TimeControlFile.tcTimeFileLocation,
15+
FileMode.OpenOrCreate,
16+
FileAccess.ReadWrite, FileShare.None);
17+
private StreamWriter streamWriter;
1318
private ListBox listBox;
1419
private List<App> apps;
1520
private Timer timer;
1621

17-
public ListController(ListBox listBox,Timer timer)
22+
public ListController(ListBox listBox, Timer timer)
1823
{
24+
streamWriter = new(fileStream);
1925
this.listBox = listBox;
2026
apps = new List<App>();
2127
this.timer = timer;
28+
StreamReader streamReader = new(fileStream);
29+
int lineNumber = 1;
30+
string name = null;
31+
int time = 0;
32+
int timeLimit = 0;
33+
while (!streamReader.EndOfStream)//读取文件,添加进程
34+
{
35+
string line = streamReader.ReadLine();
36+
if (line == "//")
37+
{
38+
if (timeLimit == 0)
39+
apps.Add(new App(name, time));
40+
else
41+
apps.Add(new LimitedApp(name, time, timeLimit));
42+
43+
lineNumber = 1;
44+
name = null;
45+
time = 0;
46+
timeLimit = 0;
47+
48+
continue;
49+
}
50+
else
51+
{
52+
if (lineNumber == 1)
53+
name = line;
54+
else if (lineNumber == 2)
55+
time = Convert.ToInt32(line);
56+
else if (lineNumber == 3)
57+
timeLimit = Convert.ToInt32(line);
58+
59+
lineNumber++;
60+
}
61+
}
62+
Refresh();
2263
}
2364

2465
/// <summary>
@@ -46,7 +87,7 @@ public void AddByName(string name)
4687
{
4788
foreach (Process process in processes)
4889
{
49-
apps.Add(new App(process.ProcessName, process.MainModule.FileName));
90+
apps.Add(new App(process.ProcessName, 0));
5091
}
5192
}
5293
catch (Exception ex)
@@ -60,15 +101,15 @@ public void AddByName(string name)
60101
/// </summary>
61102
/// <param name="name">进程名称</param>
62103
/// <param name="limitTime">限制时长(秒)</param>
63-
public void AddByName(string name,int limitTime)
104+
public void AddByName(string name, int limitTime)
64105
{
65106
timer.Stop();
66107
Process[] processes = Process.GetProcessesByName(name);
67108
try
68109
{
69110
foreach (Process process in processes)
70111
{
71-
apps.Add(new LimitedApp(process.ProcessName, process.MainModule.FileName,limitTime));
112+
apps.Add(new LimitedApp(process.ProcessName, 0, limitTime));
72113
}
73114
}
74115
catch (Exception ex)
@@ -82,19 +123,21 @@ public void AddByName(string name,int limitTime)
82123
/// </summary>
83124
public void Run()
84125
{
126+
fileStream.SetLength(0);
85127
foreach (App app in apps)//计算进程时间
86128
{
87129
if (Process.GetProcessesByName(app.Name).Length != 0)
88130
{
89131
if (app is LimitedApp)
90132
{
91-
LimitedApp limitedApp=app as LimitedApp;
92-
limitedApp.Run();
133+
LimitedApp limitedApp = app as LimitedApp;
134+
limitedApp.Run(streamWriter);
93135
}
94136
else
95-
app.Run();
137+
app.Run(streamWriter);
96138
}
97139
}
140+
streamWriter.Flush();
98141
}
99142
/// <summary>
100143
/// 移除所列表所选的进程
@@ -106,5 +149,14 @@ public void Remove()
106149
apps.RemoveAt(listBox.SelectedIndex);
107150
Refresh();
108151
}
152+
/// <summary>
153+
/// 删除所有监控
154+
/// </summary>
155+
public void RemoveAll()
156+
{
157+
timer.Stop();
158+
apps.Clear();
159+
Refresh();
160+
}
109161
}
110162
}

TimeControl/PasswordInput.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TimeControl/PasswordInput.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public PasswordInput(int passwordHash)
2121
DialogResult = DialogResult.No;
2222
}
2323

24-
private void okButton_Click(object sender, EventArgs e)
24+
private void OkButton_Click(object sender, EventArgs e)
2525
{
2626
if (passwordTextBox.Text.GetHashCode()==passwordHash)
2727
{
28-
File.Delete(PasswordFile.tcPassLocation);
28+
File.Delete(TimeControlFile.tcPassLocation);
2929
DialogResult = DialogResult.OK;
3030
Close();
3131
}

0 commit comments

Comments
 (0)