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 5182267 commit dff1964
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 18 deletions.
1 change: 1 addition & 0 deletions NotepadKeeper/NotepadKeeper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="notepadpp.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
90 changes: 72 additions & 18 deletions NotepadKeeper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,41 @@
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Linq;

namespace NotepadKeeper
{
class Program
{

public static string FindNotepadTabStatePath(string appDataLocalPath)
{
// 基础路径,其中包含了通配符*以匹配任何可能的包名后缀
string searchPattern = @"Packages\Microsoft.WindowsNotepad_*\LocalState\TabState";
string basePath = Path.Combine(appDataLocalPath, searchPattern);

// 使用 DirectoryInfo 和 GetDirectories 来搜索匹配的目录
DirectoryInfo di = new DirectoryInfo(appDataLocalPath);
if (!di.Exists)
{
throw new InvalidOperationException("指定的路径不存在");
}

// 查找符合模式的第一个目录
DirectoryInfo[] directories = di.GetDirectories("Microsoft.WindowsNotepad_*", SearchOption.AllDirectories);
if (directories.Length == 0)
{
throw new InvalidOperationException("未找到 Microsoft Windows 记事本相关的包目录");
}

// 假设我们只关心找到的第一个匹配项
var packageDir = directories.First();

// 构建最终的 TabState 路径
string tabStatePath = Path.Combine(packageDir.FullName, "LocalState", "TabState");
return tabStatePath;
}

public static void CheckAndKillProcess(string processName)
{
// 使用 GetProcessesByName 获取与指定名称相符的所有进程
Expand Down Expand Up @@ -40,7 +69,7 @@ public static void CheckAndKillProcess(string processName)
}
else
{
Console.WriteLine($"Can't find {processName}.");
Console.WriteLine($"{processName} is closed. OK.");
}
}

Expand Down Expand Up @@ -278,12 +307,12 @@ public static void dealFileType(string filePath)
// 根据第四个字节的值输出结果
if (fourthByte == 1)
{
Console.WriteLine("----[ SAVED FILE √]----");
Console.WriteLine("----( SAVED FILE √)----");
savedFile(fileStream);
}
else
{
Console.WriteLine("----[UNSAVED FILE ×]----");
Console.WriteLine("----(UNSAVED FILE ×)----");
unsavedFile(fileStream);
}
}
Expand All @@ -299,29 +328,54 @@ static void Main(string[] args)
string appDataLocalPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

// 指定要遍历的文件夹路径
string directoryPath = $@"{appDataLocalPath}\Packages\Microsoft.WindowsNotepad_8wekyb3d8bbwe\LocalState\TabState";
string directoryPath="";

// 获取目录中所有的.bin文件
string[] filePaths = Directory.GetFiles(directoryPath, "*.bin");
try
{
Console.WriteLine($"\n----[ NOTEPAD ]----\n");
directoryPath = FindNotepadTabStatePath(appDataLocalPath+ @"\Packages");
Console.WriteLine("TabState Path:" + directoryPath);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}

// 定义一个GUID的正则表达式
Regex guidRegex = new Regex(@"^[{(]?[0-9A-Fa-f]{8}[-]([0-9A-Fa-f]{4}[-]){3}[0-9A-Fa-f]{12}[)}]?$");

// 遍历所有文件
foreach (string filePath in filePaths)
try
{
// 获取文件名(不包括路径)
string fileName = Path.GetFileNameWithoutExtension(filePath);
// 获取目录中所有的.bin文件
string[] filePaths = Directory.GetFiles(directoryPath, "*.bin");

// 定义一个GUID的正则表达式
Regex guidRegex = new Regex(@"^[{(]?[0-9A-Fa-f]{8}[-]([0-9A-Fa-f]{4}[-]){3}[0-9A-Fa-f]{12}[)}]?$");

// 检查文件名是否符合GUID格式
if (guidRegex.IsMatch(fileName))
// 遍历所有文件
foreach (string filePath in filePaths)
{
Console.WriteLine($"================================================");
Console.WriteLine($"Processing file: {filePath}");
dealFileType(filePath);
// 获取文件名(不包括路径)
string fileName = Path.GetFileNameWithoutExtension(filePath);

// 检查文件名是否符合GUID格式
if (guidRegex.IsMatch(fileName))
{
Console.WriteLine($"--------------------------------");
Console.WriteLine($"Processing file: {filePath}");
dealFileType(filePath);
}
}
Console.WriteLine($"--------------------------------");

}
Console.WriteLine($"================================================");
catch (Exception ex)
{

Console.WriteLine("Error: " + ex.Message);
}



NotepadPP.get();
}
}
}
49 changes: 49 additions & 0 deletions NotepadKeeper/notepadpp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.IO;
using System.Text;

namespace NotepadKeeper
{
class NotepadPP
{
public static void get()
{

Console.WriteLine($"\n----[NOTEPAD ++]----\n");

// 动态获取当前Windows用户的用户名
string username = Environment.UserName;

// 根据用户名设置目录路径
string directoryPath = $@"C:\Users\{username}\AppData\Roaming\Notepad++\backup";

// 检查目录是否存在
if (Directory.Exists(directoryPath))
{
// 获取目录中的所有文件
string[] files = Directory.GetFiles(directoryPath);

Console.WriteLine($"Total files: {files.Length}");

// 遍历所有文件
foreach (string file in files)
{
// 读取每个文件的内容
string content = File.ReadAllText(file, Encoding.UTF8);
Console.WriteLine($"Reading file: {file}");
Console.WriteLine(content);
Console.WriteLine("--------------------------------");
}
}
else
{
Console.WriteLine($"The directory {directoryPath} does not exist.");
}

#if DEBUG
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
#endif
}
}
}

0 comments on commit dff1964

Please sign in to comment.