2012-03-26 8 views
7

Tôi đang viết một chương trình keylogger đơn giản (cho các mục đích không độc hại).Sử dụng File.AppendAllText gây ra lỗi "Quy trình không thể truy cập tệp, đã sử dụng" lỗi

Lưu ý: Đây là với .net 4.0 Client Profile

Bất cứ khi nào tôi bắt đầu chương trình, tôi nhận được lỗi này:

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

Đây là mã chính của tôi:

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

Tại sao điều này lại xảy ra? Đó là tốt khi tôi đã sử dụng bản Tuyên Bố này để ghi vào tập tin:

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

Nhưng nó ngừng làm việc khi tôi chuyển sang đơn gi ản:

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

bạn đang nhắm mục tiêu Phiên bản nào của Net? – Brannon

+0

@Brannon Đã chỉnh sửa câu hỏi –

+0

Điều này sẽ rõ ràng, nhưng bạn không có tệp nhật ký của mình mở trong một chương trình khác trong khi bạn đang cố ghi vào nó? –

Trả lời

23

tôi nhìn thấy một

File.Create(Program.file); 

không cần thiết và có thể là vấn đề trực tiếp ở đây.

Nó không chỉ tạo tệp trống mà nó còn mở và trả về đối tượng FileStream mà bạn không đóng ở bất kỳ đâu. FileStream đã mở không thể chia sẻ được vì vậy nó khiến AppendAllText() thất bại.

Chỉ cần loại bỏ dòng đó, AppendAllText() bao gồm logic để create the file if it does not already exist

+0

Điều đó có vẻ khá kỳ quặc, nhưng nó hoạt động! –