C# 无法写入文件 ("being used by another process")?

标签 c# file exception

我希望有人能向我解释为什么会这样 :)

当我使用下面的代码时,出现错误“该进程无法访问文件 'C:\test.txt',因为它正被另一个进程使用。”

我是 C# 的新手,所以我不确定发生了什么,在此先感谢!

String fileNameBefore = @"C:\\test.txt";

public void output(String hex)
{
    using (StreamWriter writer = new StreamWriter(fileNameBefore, true))
    {
        writer.Write(hex);
        writer.Close();
    }
}

最佳答案

写入前关闭文本文件。 (不在代码中...物理关闭文本文件)

关于C# 无法写入文件 ("being used by another process")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9086706/

相关文章:

c# - C#中什么情况下不为公共(public)属性生成私有(private)变量?

c# - 无法实现接口(interface)成员,因为它不是公共(public)的

c# - C 到 C# PInvoke 带指针的结构

c++ - 从文件中读取 long double,然后写入另一个文件

c - 如何使用文件输入对数组进行排序?

php - 捕获接口(interface)异常。

c# - 如何从 Azure Active Directory( native 应用程序)获取用户列表?

java - 在 JAVA 中搜索和更新文本文件中的字符串

exception - Kotlin中编号为MutableList <Int>的奇数大小返回带有迭代器的IndexOutOfBoundsException

exception - JUnit : a Successful test if an exception is catched