gpt4 book ai didi

c# - 使用 File.AppendAllText 导致 "Process cannot access the file, already in use"错误

转载 作者:太空狗 更新时间:2023-10-29 18:03:32 29 4
gpt4 key购买 nike

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

注意:这是 .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");

最佳答案

我看到一个

 File.Create(Program.file);

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

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

只需删除该行,AppendAllText() 包含 create the file if it does not already exist 的逻辑

关于c# - 使用 File.AppendAllText 导致 "Process cannot access the file, already in use"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9877362/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com