添加包
using FlaUI.UIA3;
using Application = FlaUI.Core.Application;
界面
主要代码
/// <summary>
/// 获取全部聊天记录
/// </summary>
public void GetAllMessage()
{
//列表顺序
List<string> sourceNameList = new List<string>();
List<string> nameList = new List<string>();
Process[] processes = Process.GetProcessesByName("WeChat");
if (processes.Count() != 1)
{
MessageBox.Show("微信未启动或启动多个微信");
}
else
{
//1.附加到微信进程
using (var app = Application.Attach(processes.First().Id))
{
using (var automation = new UIA3Automation())
{
//2.获取主界面
FlaUI.Core.AutomationElements.Window mainWindow = null;
try
{
mainWindow = app.GetMainWindow(automation, TimeSpan.FromSeconds(3));
if (mainWindow == null)
{
MessageBox.Show("请启动微信并将微信最小化,并重新点击开始!");
this.Invoke(new Action(() =>
{
btn_start.Enabled = true;
}));
return;
}
}
catch (Exception ex)
{
MessageBox.Show("发生错误请重新启动并使用!");
IsCreateFile(errName);
StreamWriter sw = new StreamWriter(errName);
sw.WriteLine(System.DateTime.Now.ToString("U") + ex.Source + ex.Message);
sw.Flush();
sw.Close();
return;
}
//每次的检查结果是否相等
bool isEquals = false;
while (true)
{
try
{
//如果用户将微信最小化,我们需要将微信窗体置顶激活或者最大化
if (mainWindow.AsWindow().Patterns.Window.PatternOrDefault != null)
{
//将微信窗体设置为默认焦点状态
mainWindow.AsWindow().Patterns.Window.Pattern.SetWindowVisualState(FlaUI.Core.Definitions.WindowVisualState.Normal);
}
Console.WriteLine("获取主界面");
//3.切换到通讯录
var elements = mainWindow.FindAll(FlaUI.Core.Definitions.TreeScope.Subtree, TrueCondition.Default);
var addressBook = mainWindow.FindFirstDescendant(cf => cf.ByName("聊天"));
addressBook.DrawHighlight(System.Drawing.Color.Red);
Console.WriteLine("点击聊天");
addressBook.Click();
var count = 0;
var searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("会话")).AsListBoxItem();
if (searchTextBox != null)
{
var list = searchTextBox.FindAllChildren();
#region 判断当前队列是否与上一次相等 不相等就继续检查 相等就满1分钟检查一次
//添加相同队列判断是否相等
foreach (var item in list)
{
if (item.Name.Equals("折叠置顶聊天"))
{
continue;
}
if (sourceNameList.Count == 0)
{
sourceNameList.Add(item.Name);
}
else
{
nameList.Add(item.Name);
}
}
if (!IsListEquals(sourceNameList, nameList))
{
isEquals = false;
sourceNameList.Clear();
sourceNameList.AddRange(nameList);
nameList.Clear();
}
else
{
isEquals = true;
}
#endregion
//检测是否有权进行收集记录
if (!isEquals || isDetection.WaitOne(100))
{
//将data中的内容写入test.txt中
foreach (var item in list)
{
item.Click();
var ccc = mainWindow.FindFirstDescendant(cf => cf.ByName("sendBtn"));
if (ccc != null)
{
string fileName = PATH + item.Name.Trim() + ".txt";
IsCreateFile(fileName);
string lastMessage = GetLastMessage(fileName);
if (lastMessage != null)
{
StreamWriter sw = new StreamWriter(fileName);
//是否找到和文件中匹配的最后一句话
bool isFindLastMessage = false;
var cs = mainWindow.FindFirstDescendant(cf => cf.ByName("消息")).AsListBoxItem();
var messageList = cs.FindAllChildren();
#region 第一次寻找匹配的最后一句话
//里面有上次的内容
foreach (var message in messageList)
{
if (!isFindLastMessage)
{
//没有匹配到继续匹配
try
{
if (message.Name.Equals(lastMessage))
{
isFindLastMessage = true;
}
}
catch (Exception)
{
continue;
}
}
else
{
//匹配到接着上次的继续写
sw.WriteLine(message.Name);
}
}
#endregion
#region 如果没有找到 全部写入
if (!isFindLastMessage)
{
//如果没有找到 全部写入
foreach (var message in messageList)
{
sw.WriteLine(message.Name);
}
}
#endregion
sw.Flush();
sw.Close();
}
else
{
//这是一个新的
var cs = mainWindow.FindFirstDescendant(cf => cf.ByName("消息")).AsListBoxItem();
list = cs.FindAllChildren();
StreamWriter sw = new StreamWriter(fileName);
foreach (var message in list)
{
sw.WriteLine(message.Name);
}
sw.Flush();
sw.Close();
}
Console.WriteLine("以创建" + item.Name);
Console.WriteLine("---------------------------------------");
}
else
{
Console.WriteLine(item.Name + "是空的");
}
}
}
}
mainWindow.AsWindow().Patterns.Window.Pattern.SetWindowVisualState(FlaUI.Core.Definitions.WindowVisualState.Minimized);
if (isEnd.WaitOne(10))
{
MessageBox.Show("已结束监视!");
this.Invoke(new Action(() =>
{
btn_start.Enabled = true;
}));
return;
}
Thread.Sleep(TimeSpan.FromSeconds(10));
if (isEnd.WaitOne(10))
{
MessageBox.Show("已结束监视!");
this.Invoke(new Action(() =>
{
btn_start.Enabled = true;
}));
return;
}
}
catch (Exception ex)
{
IsCreateFile(errName);
StreamWriter sw = new StreamWriter(errName);
sw.WriteLine(System.DateTime.Now.ToString("U") + ex.Source + ex.Message);
sw.Flush();
sw.Close();
MessageBox.Show("出现错误请重新启动并使用!");
return;
}
}
}
}
}
}
其他代码
public const string PATH = "D:\\微信记录集合\\";
public const string errName = PATH + "错误报告" + ".txt";
public Form1()
{
InitializeComponent();
//cs();
btn_end.Enabled = false;
}
Thread thread = null;
private void btn_start_Click(object sender, EventArgs e)
{
thread = new Thread(() => GetAllMessage());
MessageBox.Show("已启动");
thread.Start();
this.btn_start.Enabled = false;
this.btn_end.Enabled = true;
}
public AutoResetEvent isDetection = new AutoResetEvent(false);
/// <summary>
/// 时间函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
//每60秒必须触发一次检查
isDetection.Set();
}
//是否结束检测
public AutoResetEvent isEnd = new AutoResetEvent(false);
/// <summary>
/// 结束按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_end_Click(object sender, EventArgs e)
{
isEnd.Set();
btn_end.Enabled = false;
}
/// <summary>
/// 判断两个集合是否相等
/// </summary>
/// <param name="list1"></param>
/// <param name="list2"></param>
/// <returns></returns>
public bool IsListEquals(List<string> list1, List<string> list2)
{
if (list1.Count == list2.Count)
{
if (list1.SequenceEqual(list2))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// 判断为该名字的文件是否存在 如果不存在则创建
/// </summary>
/// <param name="Name">名字</param>
/// <returns>文件是否存在</returns>
public static bool IsCreateFile(string Name)
{
if (!File.Exists(Name))
{
FileStream mystream = new FileStream(Name, FileMode.Create, FileAccess.Write);
mystream.Close();
return false;
}
else
{
return true;
}
}
/// <summary>
/// 返回当前路径文件的最后一句话
/// </summary>
/// <param name="Path"></param>
/// <returns>最后一句话</returns>
public static string GetLastMessage(string Path)
{
if (File.Exists(Path))
{
string strContent = File.ReadAllText(Path);
if (strContent.Trim() == "")
{
return null;
}
else
{
strContent = strContent.Replace("\r\n", "&");
string[] strList = strContent.Split('&');
string lastMessage = strList.Last();
return lastMessage;
}
}
else
{
return null;
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
System.Environment.Exit(0);
}