13

我正在编写一个简单的键盘记录程序(用于非恶意目的)。

注意:这是 .net 4.0 客户端配置文件

每当我启动程序时,我都会收到此错误:

The process cannot access the file 'C:\Users\.. ..\bin\Debug\log.log' because it is being used by another process.

这是我的主要代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;
using System.Text;

namespace Logger
{
    public partial class MainForm : Form
    {
        public string cWin = null;
        public static string nl = Environment.NewLine;

        public MainForm()
        {
            InitializeComponent();
        }

        public static DialogResult AskOverwrite()
        {
            DialogResult result = MessageBox.Show("Log already exists. Overwrite?", 
                "Overwrite?", 
                MessageBoxButtons.YesNo, 
                MessageBoxIcon.Question);

            return result;
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {

             if (FormWindowState.Minimized == this.WindowState)
             {
                  systray.Visible = true;
                  this.Hide();    
             }
             else if (FormWindowState.Normal == this.WindowState)
             {
                  systray.Visible = false;
             }
        }

        private void SystrayMouse_DoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void HookManager_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Program.run)
            {
                output.AppendText(string.Format(e.KeyChar.ToString()));
                File.AppendAllText(Program.file, e.KeyChar.ToString());
            }
        }

        private void Start_Click(object sender, EventArgs e)
        {
            if (!Program.run)
            {
                if (File.Exists(Program.file))
                {
                    if (AskOverwrite() == DialogResult.Yes)
                    {
                        File.Create(Program.file);
                    }
                }

                Program.run = true;
                output.AppendText(string.Format("Logging started - {1}{0}", nl, System.DateTime.Now));
                File.AppendAllText(Program.file, string.Format("Logging started - {1}{0}", nl, System.DateTime.Now));
            }
            else
            {
                output.AppendText(string.Format("Logging already started!{0}", nl));
            }
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            if (Program.run)
            {
                Program.run = false;
                output.AppendText(string.Format("{0}Logging stopped - {1}{0}", nl, System.DateTime.Now));
                File.AppendAllText(Program.file, string.Format("{0}Logging stopped - {1}{0}", nl, System.DateTime.Now));
            }
            else
            {
                output.AppendText(string.Format("Logging already stopped!{0}", nl));
            }
        }

        private void getWindowTimer_Tick(object sender, EventArgs e)
        {
            if (cWin != CurrentWindow.GetActiveWindow() &&Program.run)
            {
                cWin = CurrentWindow.GetActiveWindow();
                if (cWin != null)
                {
                    output.AppendText(string.Format("{0}Window - {1}{0}", nl, cWin));
                    File.AppendAllText(Program.file, string.Format("{0}Window - {1}{0}", nl, cWin));
                }
            }
        }
    }
}

为什么会这样?当我使用此语句写入文件时很好:

using (StreamWriter sr = new StreamWriter(Program.file))
{
    sr.Write(string.Format("texthere");
}

但是当我切换到简单地使用时它停止工作:

File.AppendAllText(Program.file, string.Format("texthere");
4

1 回答 1

31

我看到一个

 File.Create(Program.file);

这是不必要的,可能是这里的直接问题。

它不仅会创建一个空文件,还会打开并返回一个您不会在任何地方关闭的 FileStream 对象。打开的 FileStream 不可共享,因此会导致 AppendAllText() 失败。

只需删除该行,AppendAllText()包括创建文件的逻辑(如果文件尚不存在)

于 2012-03-26T18:25:51.390 回答