Skip to content

Commit

Permalink
新增关闭notepad选项
Browse files Browse the repository at this point in the history
  • Loading branch information
10cks committed Apr 24, 2024
1 parent 28ee35d commit 5182267
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions NotepadKeeper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,48 @@
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace NotepadKeeper
{
class Program
{

public static void CheckAndKillProcess(string processName)
{
// 使用 GetProcessesByName 获取与指定名称相符的所有进程
Process[] runningProcesses = Process.GetProcessesByName(processName);

if (runningProcesses.Length > 0)
{
Console.WriteLine($"Process {processName} is running!");

// 遍历找到的所有进程并尝试关闭它们
foreach (Process proc in runningProcesses)
{
try
{
// 关闭前先确保进程没有被关闭
if (!proc.HasExited)
{
proc.Kill(); // 强制关闭进程
proc.WaitForExit(); // 等待进程关闭
Console.WriteLine($"Process {proc.ProcessName} (ID: {proc.Id}) is closed.");
}
}
catch (Exception ex)
{
// 处理可能出现的异常,例如没有足够权限关闭进程
Console.WriteLine($"Can't close {proc.ProcessName} (ID: {proc.Id}): {ex.Message}");
}
}
}
else
{
Console.WriteLine($"Can't find {processName}.");
}
}

/*
已保存文件结构:
大于0x80的数据:[Magic Header(3bytes)] [4th byte: unsaved/saved file] [5th byte: filePathStr length] [filePath string] [content length(1 or 2 bytes)] [05 01] [padding(53 bytes)] [content] [6 bytes]
Expand Down Expand Up @@ -256,6 +292,9 @@ public static void dealFileType(string filePath)

static void Main(string[] args)
{
// 检查进程是否运行
CheckAndKillProcess("notepad");

// 获取当前用户的AppData\Local文件夹路径
string appDataLocalPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

Expand Down

0 comments on commit 5182267

Please sign in to comment.