如何使用C#查找最近使用不支持ABOUTCHACHE的浏览器打开的文件请给出完整代码
以下是使用C#查找最近使用的不支持ABOUT:CHACHE的浏览器打开的文件的完整代码:
using Microsoft.Win32;
using System;
using System.IO;
class Program
{
static void Main()
{
string browserPath = GetDefaultBrowserPath();
string[] recentFiles = GetRecentlyOpenedFiles(browserPath);
foreach (string file in recentFiles)
{
Console.WriteLine(file);
}
}
static string GetDefaultBrowserPath()
{
string browserPath = string.Empty;
using (RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice"))
{
if (userChoiceKey != null)
{
object progIdValue = userChoiceKey.GetValue("Progid");
if (progIdValue != null)
{
using (RegistryKey progIdKey = Registry.ClassesRoot.OpenSubKey(progIdValue.ToString() + @"\shell\open\command"))
{
if (progIdKey != null)
{
browserPath = progIdKey.GetValue(null).ToString().ToLower().Replace("\"", "");
if (!browserPath.EndsWith("exe"))
{
browserPath = browserPath.Substring(0, browserPath.LastIndexOf(".exe") + 4);
}
}
}
}
}
}
return browserPath;
}
static string[] GetRecentlyOpenedFiles(string browserPath)
{
string historyFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Microsoft\Windows\Recent\CustomDestinations");
string[] recentFiles = Directory.GetFiles(historyFilePath, "*.customDestinations-ms");
foreach (string file in recentFiles)
{
string tempFilePath = Path.GetTempFileName();
File.Move(file, tempFilePath);
try
{
using (StreamReader reader = new StreamReader(tempFilePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line.Contains(browserPath))
{
string[] parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string url = parts[parts.Length - 1];
if (url.Contains("file:///"))
{
yield return url.Replace("file:///", "");
}
}
}
}
}
finally
{
File.Move(tempFilePath, file);
}
}
}
}
此代码使用了Microsoft.Win32命名空间中的Registry类来获取默认浏览器的路径,并使用System.IO命名空间中的Directory和File类来查找最近打开的文件。
请注意,此代码只适用于Windows操作系统
原文地址: https://www.cveoy.top/t/topic/ineK 著作权归作者所有。请勿转载和采集!